字节流的测试
package com.ran;
import java.io.*;
import java.util.Collections;
public class Ran {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
try {
File file = new File("hello.txt");
fileInputStream = new FileInputStream(file);
byte[] buffer=new byte[5];
int len;
while ((len=fileInputStream.read(buffer))!=-1){
String str = new String(buffer, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
结论:
- 对于文本文件(.txt .java .c .cpp…),使用字符流处理
- 对于非文本文件(.jpg .mp3 .mp4 .avi .doc .ppt …),使用字节流处理