In this tutorials we are going to see how to convert String to InputStream using methods available in Java IO. BufferedReader is used to get String again from InputStream.
Create project called JavaMisc.
Create package called com.javatutorialscorner.io
Create java class called StringToInputStream under com.javatutorialscorner.io package.
StringToInputStream.java
package com.javatutorialscorner.io;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StringToInputStream {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String jtc = "This is Test String by Java Tutorials Corner";
InputStream is = new ByteArrayInputStream(jtc.getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String temp;
try {
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Now run the program see the following output in console.
This is Test String by Java Tutorials Corner
0 comments:
Post a Comment