1 // 使用文件字节输入流一次性读取文件全部字节。可以解决乱码问题
2 public static void main(String[] args) throws Exception {
3 // 创建一个文件字节输入流管道与源文件接通
4 File file = new File("src/test.txt");
5 InputStream fis = new FileInputStream(file);
6 System.out.println(file.getAbsolutePath());
7
8 // 定义一个字节数组与文件大小一样大
9 byte[] bytes = new byte[(int) file.length()];
10 int len = fis.read();
11 System.out.println("读取了多少字节:" + len);
12 System.out.println("文件大小:" + file.length());
13 System.out.println(new String(bytes));
14
15 // 读取文件全部字节
16 byte[] bytes1 = fis.readAllBytes();
17 System.out.println(new String(bytes1));
18
19 }