ตัวอย่าง
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 InputStreamToStringExample {
/**
* @param args
*/
public static void main(String[] args) {
// intilize an InputStream
InputStream is = new ByteArrayInputStream(
"file content..example ".getBytes());
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(sb.toString());
System.out.println("InputStream To String Example Is Done.");
}
}
ผลลัพธ์ที่ได้คือ
file content..example InputStream To String Example Is Done.
0 comments:
Post a Comment