字节(byte):是计算机中数据处理的基本单位,习惯上用大写B来表示,1B(byte,字节)=8bit(位)
字符(char):是指计算机中使用的字母、数字、字和符号。依据字符不同的编码格式,每个字符单位对应的字节数是不一样的。
字节和字符的关系: (字符是用字节存储的,字节是计算机识别的语言(ASCILL)
字节是计算机的基本储存单位,字符是人们为了便于记录而使用的记号等,字符在计算机中是由字节存储的
字符是根据字符集指定的编码标准而变成字节存储的


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