62,615
社区成员
发帖
与我相关
我的任务
分享
public class FileReadOp {
public static void main(String[] args) {
FileInputStream fileInput = null;
try {
/**
* 在普通的Java工程中建立了一个test.txt文本,
* 内容:"test我喜欢深圳"
*/
fileInput = new FileInputStream("test.txt");
//缓冲区以前是new byte[1024],今天故意改成5
byte[] buffer = new byte[5];
String test = "test我喜欢深圳";
byte[] testBytes = test.getBytes();
System.out.println("文本内容总字节数:" + testBytes.length);
//计数,缓冲区很小,while循环次数统计
int count = 0;
int number = 0;
while((number = fileInput.read(buffer)) > 0){
System.out.println(new String(buffer,0,number));
System.out.println("循环次数:" + ++count);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
fileInput.close();
fileInput = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文本内容总字节数:14
test我
循环次数:1
喜欢深
循环次数:2
圳
循环次数:3