标签:reader ring row app 文件输入 system 内存流 pen fileinput
import java.io.*;
import org.junit.Test;
public class ByteArrayOutputStreamTest {
@Test
public void test1() throws Exception {
//创建文件输入字节流 文件在model下
FileInputStream fis = new FileInputStream("a.txt");
//调用方法出入字节流
String info = readStringFromInputStream(fis);
System.out.println(info);
}
private String readStringFromInputStream(FileInputStream fis) throws IOException {
/* // 方式一:可能出现乱码
StringBuilder content = new StringBuilder();
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
content.append(new String(buffer));
}
return content.toString();*/
/* // 方式二:BufferedReader
//创建字符缓冲输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
//创建char数组
char[] buf = new char[10];
int len;
//创建可变字符串
StringBuilder str = new StringBuilder();
while ((len = reader.read(buf)) != -1) {
//每次在末尾添加
// str.append(new String(buf, 0, len));
str.append(new String(buf));
}
return str.toString();*/
// 方式三:避免出现乱码
//创建内存流 字节数组输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10];
int len;
while ((len = fis.read(buffer)) != -1) {
// baos.write(buffer, 0, len);
baos.write(buffer);
}
return baos.toString();
}
}
标签:reader ring row app 文件输入 system 内存流 pen fileinput
原文地址:https://www.cnblogs.com/zk2020/p/14258370.html