Java读取文件内容(多个字节)
package com.ycy1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) {
// 读取文件,多个字节
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("E:" + File.separator + "test.txt");
byte[] b = new byte[4];
// fileInputStream.read(b)返回有效字节的长度
//int read = fileInputStream.read(b);
//System.out.println("字节长度:" + read);
int len = 0;
while ((len = fileInputStream.read(b)) != -1) {
String string = new String(b, 0, len);
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}