字节(byte):是计算机中数据处理的基本单位,习惯上用大写B来表示,1B(byte,字节)=8bit(位)
    字符(char):是指计算机中使用的字母、数字、字和符号。依据字符不同的编码格式,每个字符单位对应的字节数是不一样的。

    字节和字符的关系: (字符是用字节存储的,字节是计算机识别的语言(ASCILL)
    字节是计算机的基本储存单位,字符是人们为了便于记录而使用的记号等,字符在计算机中是由字节存储的
    字符是根据字符集指定的编码标准而变成字节存储的
    image.png
    image.pngimage.png

    1. package com.itheima.d4_byte_stream;
    2. import java.io.*;
    3. public class FileInputStreamDemo01 {
    4. public static void main(String[] args) throws IOException {
    5. // 1. 创建一个文件字节输入流管道与源文件接通
    6. // 以多态的形式写 子类对象用父类接收
    7. // InputStream is = new FileInputStream(new File("file-io-app\\src\\data.txt"));
    8. // 简化写法:
    9. InputStream is = new FileInputStream("file-io-app\\src\\data.txt");
    10. // // 每次读取一滴水
    11. // // 读取data.txt文件的一个字节 英文占一个字节 data.txt 里面存储的是 ab4
    12. // int b1 = is.read();
    13. // // 返回的是int数字类型,用看字符类型用char转换
    14. // System.out.println(b1); // 97 这是字节值 转换成字符用char
    15. // System.out.println((char) b1);
    16. //
    17. // // IO流读写像流水一样,一直往下流,现在读的是 b字符
    18. // int b2 = is.read();
    19. // // 返回的是int数字类型,用看字符类型用char转换
    20. // System.out.println(b2); // 98 这是字节值 转换成字符用char
    21. // System.out.println((char) b2);
    22. //
    23. // // 现在读取的是 3
    24. // int b3 = is.read();
    25. // // 返回的是int数字类型,用看字符类型用char转换
    26. // System.out.println(b3); // 3 这是字节值 转换成字符用char
    27. // System.out.println((char) b3);
    28. //
    29. // // 读取不到,返回 -1
    30. // int b4 = is.read();
    31. // // 返回的是int数字类型,用看字符类型用char转换
    32. // System.out.println(b4); // 这是字节值 转换成字符用char
    33. // System.out.println((char) b4);
    34. // 3. 使用循环改进
    35. // 定义一个变量记录每次读取的字节 a b 3 爱
    36. // o o o [ooo] 爱这个中文是占用三个字节,下面的循环读到一半会断开,所以乱码
    37. int b;
    38. while ((b = is.read()) != -1) { // 当IO流读取到的值不是-1 代表能读到数据,就返回该字节
    39. // 这里不要换行,因为文件自带换行
    40. System.out.print((char) b); // 将该字节(ASCILL值)转换为字符(原内容)
    41. }
    42. }
    43. }