缓冲区输入流也和文件输入流差不多,直接看代码:
1 package com.hw.file0221;
2
3 import java.io.BufferedInputStream;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7
8 public class Demo02TestBufferedInputStream {
9 public static void main(String[] args) {
10
11 BufferedInputStream input = null;
12 FileInputStream fileinput;
13 try {
14 fileinput = new FileInputStream("F://骚操作//demotest01.txt");
15 input = new BufferedInputStream(fileinput);
16
17 int data = input.read(); //按字节
18 System.out.println((char)data);
19 data = input.read();
20 System.out.println((char)data);
21 data = input.read();
22 System.out.println((char)data);
23
24 byte[] info = new byte[1024]; //按数组
25 int length = input.read(info);
26 String str = new String(info,0,length);
27 System.out.println(str);
28
29 } catch (IOException e) {
30 // TODO Auto-generated catch block
31 e.printStackTrace();
32 }finally{
33 try {
34 if(input != null){
35 input.close();
36 }
37 } catch (IOException e) {
38 // TODO Auto-generated catch block
39 e.printStackTrace();
40 }
41 }
42 }
43 }
读取文件也是可以按字节或者按照字节数组来。
原文:https://www.cnblogs.com/EvanTheGreat/p/14428677.html