Java中String與BufferedReader、InputStream轉換
1、String –> InputStream
InputStrem is = new ByteArrayInputStream(str.getBytes());
或者
ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes());
2、InputStream–>String
inputStream input = null;
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = input.read(b)) != -1;) {
out.append(new String(b, 0, n)); //這個可以用來讀取檔案內容 並且檔案內容有中文讀取出來也不會亂碼
}
return out.toString();
3、Reader –>String
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = " ";
while ((line = in.readLine()) != null){
buffer.append(line);
}
return buffer.toString();
4、String–>Reader
Reader reader = null;
BufferedReader r = new BufferedReader(reader);
StringBuilder b = new StringBuilder();
String line;
while((line=r.readLine())!=null) {
b.append(line);
b.append(“\r\n”);
}
return b.toString();
---------------------
作者:Jay_1989
來源:CSDN
原文:https://blog.csdn.net/Jay_1989/article/details/70154068
版權宣告:本文為博主原創文章,轉載請附上博文連結!