* 字节输入流操作步骤:
* A:创建字节输入流对象
* B:调用read()方法读取数据,并把数据显示在控制台
* C:释放资源
*
* 读取数据的方式:
* A:int read():一次读取一个字节
* B:int read(byte[] b):一次读取一个字节数组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class FileInputStreamDemo { 2 public static void main(String[] args) throws IOException { 3 // FileInputStream(String name) 4 // FileInputStream fis = new FileInputStream("fis.txt"); 5 FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java"); 6 7 // // 调用read()方法读取数据,并把数据显示在控制台 8 // // 第一次读取 9 // int by = fis.read(); 10 // System.out.println(by); 11 // System.out.println((char) by); 12 // 13 // // 第二次读取 14 // by = fis.read(); 15 // System.out.println(by); 16 // System.out.println((char) by); 17 // 18 // // 第三次读取 19 // by = fis.read(); 20 // System.out.println(by); 21 // System.out.println((char) by); 22 // // 我们发现代码的重复度很高,所以我们要用循环改进 23 // // 而用循环,最麻烦的事情是如何控制循环判断条件呢? 24 // // 第四次读取 25 // by = fis.read(); 26 // System.out.println(by); 27 // // 第五次读取 28 // by = fis.read(); 29 // System.out.println(by); 30 // //通过测试,我们知道如果你读取的数据是-1,就说明已经读取到文件的末尾了 31 32 // 用循环改进 33 // int by = fis.read(); 34 // while (by != -1) { 35 // System.out.print((char) by); 36 // by = fis.read(); 37 // } 38 39 // 最终版代码 40 int by = 0; 41 // 读取,赋值,判断 42 while ((by = fis.read()) != -1) { 43 System.out.print((char) by); 44 } 45 46 // 释放资源 47 fis.close(); 48 } 49 }