创建文件字节输入流管道并与源文件接通:
InputStream is = new FileInputStream("file\\\\src\\\\data.txt");
读取一个字节返回:(每次读取一滴水)
int b1 = is.read();
System.out.println((char)b1);
int b2 = is.read();
System.out.println((char)b2);
int b3 = is.read();
System.out.println((char)b3);
int b4 = is.read();//读取完毕返回-1
System.out.println(b4);
使用循环改进(缺点:性能低,无法处理中文)
				int b;
        while (( b = is.read() ) != -1){
            System.out.print((char) b);
        }
public class FileInputStreamDemo02 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字节输入流管道与源文件接通
        InputStream is = new FileInputStream("file-io-app/src/data02.txt");
				//ab3abccd
        // 2、定义一个字节数组,用于读取字节数组
       byte[] buffer = new byte[3]; // 3B
       int len = is.read(buffer);
       System.out.println("读取了几个字节:" + len);
       String rs = new String(buffer);
       System.out.println(rs);//ab3
       int len1 = is.read(buffer);
       System.out.println("读取了几个字节:" + len1);
       String rs1 = new String(buffer);
       System.out.println(rs1);
       // buffer = [a b c]
       // buffer = [a b c]  ==>  [c d c]
       int len2 = is.read(buffer);
       System.out.println("读取了几个字节:" + len2);
       // 读取多少倒出多少
       String rs2 = new String(buffer,0 ,len2);//从数组的 0 开始,一直读到 len2
       System.out.println(rs2);
       int len3 = is.read(buffer);
       System.out.println(len3); // 读取完毕返回-1
        // 3、改进使用循环,每次读取一个字节数组
        byte[] buffer = new byte[3];
        int len; // 记录每次读取的字节数。
        while ((len = is.read(buffer)) != -1) {
            // 读取多少倒出多少
            System.out.print(new String(buffer, 0 , len));
        }
    }
}
<aside> 💡 用字节流读取中文时,一定要注意每个中文字符占 3 个字节。
每次读取一个字节数组,读取的性能得到了提升,但是读取中文字符输出无法避免乱码问题。
</aside>
<aside> ⚠️ 如果文件过大,可能导致内存溢出。
</aside>
public class FileInputStreamDemo03 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字节输入流管道与源文件接通
        File f = new File("file-io-app/src/data03.txt");
        InputStream is = new FileInputStream(f);
        // 2、定义一个字节数组与文件的大小刚刚一样大。
       byte[] buffer = new byte[(int) f.length()];
       int len = is.read(buffer);
       System.out.println("读取了多少个字节:" + len);
       System.out.println("文件大小:" + f.length());
       System.out.println(new String(buffer));
        // 读取全部字节数组
        byte[] buffer = is.readAllBytes();
        System.out.println(new String(buffer));
    }
}
<aside> ⚠️ 以上三种方式可得知,字节流用来读入字符始终不合适。
</aside>
public class OutputStreamDemo04 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字节输出流管道与目标文件接通
        OutputStream os = new FileOutputStream("file-io-app/src/out04.txt" , true); // 追加数据管道
//        OutputStream os = new FileOutputStream("file-io-app/src/out04.txt"); // 先清空之前的数据,写新数据进入
        // 2、写数据出去
        // a.public void write(int a):写一个字节出去
        os.write('a');
        os.write(98);
        os.write("\\r\\n".getBytes()); // 换行
        // os.write('徐'); // [ooo]
        // b.public void write(byte[] buffer):写一个字节数组出去。
        byte[] buffer = {'a' , 97, 98, 99};
        os.write(buffer);
        os.write("\\r\\n".getBytes()); // 换行
        byte[] buffer2 = "我是中国人".getBytes();
//        byte[] buffer2 = "我是中国人".getBytes("GBK");
        os.write(buffer2);
        os.write("\\r\\n".getBytes()); // 换行
        // c. public void write(byte[] buffer , int pos , int len):写一个字节数组的一部分出去。
        byte[] buffer3 = {'a',97, 98, 99};
        os.write(buffer3, 0 , 3);
        os.write("\\r\\n".getBytes()); // 换行
        // os.flush(); // 写数据必须,刷新数据 可以继续使用流
        os.close(); // 释放资源,包含了刷新的!关闭后流不可以使用了
    }
}
<aside> ⚠️ 注意事项:
true 即可getBytes(); 方法可以指定编码
</aside>public class CopyDemo05 {
    public static void main(String[] args) {
        try {
            // 1、创建一个字节输入流管道与原视频接通
            InputStream is = new FileInputStream("file-io-app/src/out04.txt");
            // 2、创建一个字节输出流管道与目标文件接通
            OutputStream os = new FileOutputStream("file-io-app/src/out05.txt");
            // 3、定义一个字节数组转移数据
            byte[] buffer = new byte[1024];
            int len; // 记录每次读取的字节数。
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0 , len);
            }
            System.out.println("复制完成了!");
            // 4、关闭流。
            os.close();
            is.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
<aside> ⚠️ 注意事项:
1kb (1024)