int len = 0;
while ((len = fis.read()) != -1){
fis.read();
}
逐一将文档中的字符转化为字节输出
每识别一个字节光标往下移动一位
识别到最后一位时输出-1
.
文件夹内容
abc
输出结果:
97,98,99,
.
.
字节输入流一次读取多个字符
byte[] bytes = new byte[2];设置每次读取的长度
int len = fis.read(bytes);读取的有效个数
System.out.println(len); 2
System.out.println(new String(bytes)); AB
int len1 = fis.read(bytes);
System.out.println(len1); 2
System.out.println(new String(bytes)); CD
int len2 = fis.read(bytes);
System.out.println(len2); 2
System.out.println(new String(bytes)); EF
int len3 = fis.read(bytes);
System.out.println(len3); -1
System.out.println(new String(bytes)); EF
int len4 = fis.read(bytes);
System.out.println(len4); -1
System.out.println(new String(bytes)); EF
.
当不知道文件中有多少个字节时
new String(bytes,0,len)
截取有效部分输出,以防浪费内存
.
.
BufferedReader
readLine()读取一行字符串
.
.
InputStreamReader读取文件中的字符
InputStreamReader isr = new InputStreamReader(new FileInputStream("IO\\a.txt"),"Gbk");
int len = 0;
while((len = isr.read()) != -1){
System.out.println((char)len);
}
isr.close();