ตัวอย่าง
package demo.file;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StringToInputStreamExample {
/**
* @param args
*/
public static void main(String[] args) {
String str = "This is a string to InputStream example";
// convert String into InputStream
InputStream is = new ByteArrayInputStream(str.getBytes());
// read it with BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ผลลัพธ์ที่ได้คือ
This is a string to InputStream example
0 comments:
Post a Comment