- 一,IO流的概念
- 二、基本分类
- 三、IO流的框架结构(*)
- 四、相关流的详解
- (五)FileOutputStream类(重点)
- (六) FileInputStream类(重点)
- (八)BufferedInputStream类(重点)
- (九)缓冲流实现文件的拷贝(开发一般就用这个)
- (十)BufferedWriter类(重点)
- (十一) BufferedReader类(重点)
- (十二) PrintStream类
- (十三) PrintWriter类
- (十八) OutputStreamWriter类
- (十九) InputStreamReader类
- (二十)字符编码
- (二十一)DataOutputStream类(了解)
- (二十二) DataInputStream类(了解)
- (二十三)ObjectOutputStream类(重点)
- (二十五) ObjectInputStream类(重点)
- (二十四) RandomAccessFile类
- 笔试考点*
一,IO流的概念
IO就是Input和Output的简写,也就是输入和输出的含义。
IO流就是指读写数据时像流水一样从一端流到另外一端,因此得名为“流”。
二、基本分类
(1)按照读写数据的基本单位不同,分为 字节流 和 字符流。
其中字节流主要指以字节为单位进行数据读写的流,可以读写任意类型的文件。
其中字符流主要指以字符(2个字节)为单位进行数据读写的流,只能读写文本文件。
(2)按照读写数据的方向不同,分为 输入流 和 输出流(站在程序的角度)。
其中输入流主要指从文件中读取数据内容输入到程序中,也就是读文件。
其中输出流主要指将程序中的数据内容输出到文件中,也就是写文件。
(3)按照流的角色不同分为节点流和处理流。
其中节点流主要指直接和输入输出源对接的流。
其中处理流主要指需要建立在节点流的基础之上的流。
三、IO流的框架结构(*)
字节(Byte):字节是通过网络传输信息(或在硬盘或内存中存储信息)的单位。字节是计算机信息技术用于计量存储容量和传输容量的一种计量单位,1个字节等于8位二进制,它是一个8位的二进制数,是一个很具体的存储空间。
字符(char):人们使用的记号,抽象意义上的一个符号。 ‘1’, ‘中’, ‘a’, ‘$’, ‘¥’, ……一个字符(1char = 2byte = 16bit),java的char是unicode编码,占两个字节,一个字节占计算机里的8位(bit),按二进制换算,它的范围是-128 to 127。
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。
程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。
[
](https://blog.csdn.net/u012566290/article/details/50351312)
四、相关流的详解
(一) FileWriter类(重点)
(1)基本概念
java.io.FileWriter类主要用于将文本内容写入到文本文件。
(2)常用的方法
(3)基本实现
package Task_neuedu;import java.io.FileWriter;public class FileReaderTest {public static void main(String[] args) {//若文件不存在,则该流会新建空的文件//若文件存在,则该流会清空文件里的内容//根据参数名,构造一个对象FileWriter fw=new FileWriter("d:/aa.txt");//将文本内容写到文件fw.write('a');//关闭流fw.close();}}
示例图:
加上异常机制:
package Task_neuedu;import java.io.FileWriter;import java.io.IOException;public class FileReaderTest {public static void main(String[] args) {//创造一个对象FileWriter fw= null;try {//若文件不存在,则该流会新建空的文件//若文件存在,则该流会清空文件里的内容// fw = new FileWriter("d:/aa2.txt");//使用这个方法,若文件存在,则保留文件内容//若文件不存在,则创建新的空文件fw=new FileWriter("d:/aa2.txt",true);//将文本内容写到文件fw.write("你好 io流");} catch (IOException e) {e.printStackTrace();}finally {if (null!=fw) {try {fw.close();} catch (IOException e) {e.printStackTrace();}}}}}
写入char数组的内容
fw.write("你好 io流");char [] arr={'h','e','l','l','o'};fw.write(arr,1,2); //e l l写入到文件System.out.println("写入成功!");
(二)FileReader类(重点)
(1)基本概念
java.io.FileReader类主要用于从文本文件读取文本数据内容。
(2)常用的方法
(3)基本方法的使用
package Task_neuedu;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class FileReaderTest2 {public static void main(String[] args) {FileReader fr= null;try {/*单个读取fr = new FileReader("d:/aa2.txt");int a= 0;try {a = fr.read();} catch (IOException e) {e.printStackTrace();}System.out.println((char)a);*/fr=new FileReader("d:/aa2.txt");/* 遍历读取,直到读取到最后一个字符int res=0;while ((res=fr.read())!=-1){System.out.println((char)res);缺点:以单个字节为单位进行拷贝时,也就是每读取一个字节就写入一个字节文件稍大时(如一个50M的视频),拷贝的效率很低}*///定义一个一定长度数组存放读入的数据char [] arr2=new char[10];int res2= fr.read(arr2,1,3); //读取到arr2中,存储的位置是1号索引位置到3号索引位置for (char res:arr2){System.out.println(res); //遍历输出存储进arr2的内容}//int res=fr.read(arr2);将内容全部存储到数组中,内存不够,则存储前面一部分内容} catch (IOException e) {e.printStackTrace();}finally {if (null!=fr){try {fr.close(); //关闭流} catch (IOException e) {e.printStackTrace();}}}}}
(三)文件字符流实现文件的拷贝
(1),基本拷贝—拷贝:
注意看看字符流实现文件拷贝的缺点!
FileReader: java.io.FileReader类主要用于从文本文件读取文本数据内容。
FileWriter: java.io.FileWriter类主要用于将文本内容写入到文本文件。
拷贝文件:
(1)先读取文本文件内容
(2)写入到另一文本文件
(3)完成拷贝
package Task_neuedu;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/*文件字符流实现文件的拷贝*/public class FileCharCopyTest {public static void main(String[] args) {//创建FileReader对象与d:/aa2.txt关联,aa2.txt为输入源文件FileReader fr= null;//创建FileReader对象与d:/b.txt关联,b.txt为输出目标文件FileWriter fw= null;try {fr = new FileReader("d:/aa2.txt"); //读取源文件fw = new FileWriter("d:/b.txt"); //输出目标文件//把读取的内容写入到文件中System.out.println("拷贝中...");int res;while ((res=fr.read())!=-1){ //不断地从输入流当中读取内容,往输入流当中写入内容fw.write(res);}System.out.println("拷贝成功");} catch (IOException e) {e.printStackTrace();}finally {if (null!=fw){try {fw.close(); //慢创建的先关闭} catch (IOException e) {e.printStackTrace();}}if (null!=fr){try {fr.close(); //先创建的慢关闭} catch (IOException e) {e.printStackTrace();}}}}}
(四)文件字节流实现文件的拷贝
首先,看一下例子,如果用字符流的方式对图片进行拷贝,则拷贝失败,拷贝的图片“您的照片已损坏”
说明字符流只能对文本内容进行读取操作,而其他内容用字节流来操作。
==》文件字节流实现文件的拷贝,文件字节流的概念和基本方法在案例底下有介绍!FileInputStream和FileOutputStream.
package Task_neuedu;import java.io.*;public class FileStreamTest {public static void main(String[] args) {//创建FileInputStream与d:/io框架图.jpg关联FileInputStream fi= null;FileOutputStream fo=null;try {fi = new FileInputStream("d:/io框架图.jpg");//创建FileOutputStream与d:/io框架副本.jpg关联fo=new FileOutputStream("d:/io框架图副本.jpg");System.out.println("开始拷贝。。。");//读取输入的流写入到输出流中//以单个字节为单位进行拷贝时,也就是每读取一个字节就写入一个字节//缺点:文件稍大时,拷贝的效率很低int res=0;while ((res= fi.read())!=-1){fo.write(res);}System.out.println("拷贝成功!");} catch (IOException e) {e.printStackTrace();}//关闭输入输出流try {if (null != fo) {fo.close();}if(null!=fi) {fi.close();}} catch (IOException e) {e.printStackTrace();}}}
(1)两种拷贝方式的缺点:
方式一:
//以单个字节为单位进行拷贝时,也就是每读取一个字节就写入一个字节//缺点:文件稍大时(如一个50M的视频),拷贝的效率很低int res=0;while ((res= fi.read())!=-1){fo.write(res);}
方式一的方式好比买50个鸡蛋,每次买一个鸡蛋回到家,再去买一个鸡蛋,跑五十次,你说效率低不低?
方式二:
/* 方式二,使用一个缓冲区都一次性把数据装回去比第一种方式快了很多缺点:每次都要准备很大的框去装鸡蛋,比较吃内存,物理内存没那么多若文件过大时,无法申请与文件大小一样的缓冲区*/int available = fi.available(); //获取文件大小,单位:字节byte [] bytes=new byte[available]; //用byte数组装数据int read = fi.read(bytes);System.out.println(available);System.out.println(read);fo.write(bytes);System.out.println("拷贝成功!");
(2)方式三(推荐,分批运数据!)
/*方式三:给适当大小的缓冲区,分批次买鸡蛋** */ byte [] bytes=new byte[1024];int res=0;if ((res=fi.read())!=-1){fo.write(bytes,0,res); //res就是读取的字节,读取到最后一个字节时返回负一}System.out.println("拷贝成功");
(五)FileOutputStream类(重点)
(1)基本概念
java.io.FileOutputStream类主要用于将图像数据之类的原始字节流写入到输出流中。
(2)常用的方法
(六) FileInputStream类(重点)
(1)基本概念
java.io.FileInputStream类主要用于从输入流中以字节流的方式读取图像数据等。
(2)常见的方法

package Task_neuedu;import java.io.*;public class FileStreamTest {public static void main(String[] args) {long l1=System.currentTimeMillis();//创建FileInputStream与d:/io框架图.jpg关联FileInputStream fi= null;FileOutputStream fo=null;try {fi = new FileInputStream("d:/01.IO流的概念和分类.pcwlenv");//创建FileOutputStream与d:/io框架副本.jpg关联fo=new FileOutputStream("d:/01.IO流的概念和分类2.pcwlenv");System.out.println("开始拷贝。。。");//读取输入的流写入到输出流中/* 方式一:使用这种方式,效率低下,一个字节一个字节读int res=0;while ((res= fi.read())!=-1){fo.write(res);}*//* 方式二,使用一个缓冲区都一次性把数据装回去比第一种方式快了很多缺点:每次都要准备很大的框去装鸡蛋,比较吃内存,物理内存没那么多若文件过大时,无法申请与文件大小一样的缓冲区int available = fi.available(); //获取大小,单位:字节byte [] bytes=new byte[available]; //用byte数组装数据int read = fi.read(bytes);System.out.println(available);System.out.println(read);fo.write(bytes);System.out.println("拷贝成功!");*//*方式三:给适当大小的缓冲区,分批次买鸡蛋** */ byte [] bytes=new byte[1024];int res=0;if ((res=fi.read())!=-1){fo.write(bytes,0,res); //res就是读取的字节,读取到最后一个字节时返回负一}System.out.println("拷贝成功");} catch (IOException e) {e.printStackTrace();}//关闭输入输出流try {if (null != fo) {fo.close();}if(null!=fi) {fi.close();}} catch (IOException e) {e.printStackTrace();}long l2=System.currentTimeMillis();System.out.println("使用字节流拷贝文件:"+(l2-l1)+"毫秒");}}
(七)缓冲流BufferedOutputStream类(重点)
(1)基本概念
java.io.BufffferedOutputStream类主要用于描述缓冲输出流,此时不用为写入的每个字节调用底层
系统。
(2)常用的方法
(八)BufferedInputStream类(重点)
(1)基本概念
java.io.BufffferedInputStream类主要用于描述缓冲输入流。
(2)常用的方法
(九)缓冲流实现文件的拷贝(开发一般就用这个)
package Task_neuedu;import java.io.BufferedInputStream;import java.io.*;public class BufferdByteCopy {public static void main(String[] args) {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {//创建一个BufferdInputStream对象输入文件bis = new BufferedInputStream(new FileInputStream("d:/01.IO流的概念和分类.pcwlenv"));//创建一个BufferdInputStream对象读取文件bos = new BufferedOutputStream(new FileOutputStream("d:/01.IO流的概念和分类3.pcwlenv"));System.out.println("拷贝中>>>...");//创建一个缓冲区byte[] bytes = new byte[1024];int res = 0;if ((res = bis.read()) != -1) {bos.write(bytes, 0, res); //把读取的数据存储到bytes中,写入一段读一段~缓冲区起到中介的作用}System.out.println("拷贝成功!");} catch (IOException e) {e.printStackTrace();} finally {}//关闭流if (null != bos) {try {bos.close();} catch (IOException e) {e.printStackTrace();}}if (null != bis) {try {bis.close();} catch (IOException e) {e.printStackTrace();}}}}
可以使用Sytem的方法测试效率———Long l2 = System.currentTimeMillis();
(十)BufferedWriter类(重点)
(1)基本概念
java.io.BufffferedWriter类主要用于写入单个字符、字符数组以及字符串到输出流中。
(2)常用的方法
(十一) BufferedReader类(重点)
(1)基本概念
java.io.BufffferedReader类用于从输入流中读取单个字符、字符数组以及字符串。
(2)常用的方法

缓冲字节流实现拷贝!—文本文件
package Task_neuedu;import java.io.*;public class BufferdBye {public static void main(String[] args) {BufferedReader br= null;BufferedWriter bw= null;try {//构造缓冲流对象写入数据br = new BufferedReader(new FileReader("d:/aa2.txt"));//装饰模式//构造缓冲字节流读取数据bw = new BufferedWriter(new FileWriter("d:/a.txt"));System.out.println("开始拷贝");//不断地从输入流中读取数据到输出流String str=null;if((str=br.readLine())!=null){bw.write(str);bw.newLine();}System.out.println("拷贝完成");} catch (IOException e) {e.printStackTrace();} finally {if (null!=bw){try {bw.close();} catch (IOException e) {e.printStackTrace();}}if (null!=br){try {br.close();} catch (IOException e) {e.printStackTrace();}}}}}
缓冲字符流~读取字符串
bw.newLine(); //当前系统中的行分隔符是\r\n
(十二) PrintStream类
(1)基本概念
java.io.PrintStream类主要用于更加方便地打印各种数据内容。
(2)常用的方法
(十三) PrintWriter类
(1)基本概念
java.io.PrintWriter类主要用于将对象的格式化形式打印到文本输出流。
(2)常用的方法

- 案例题目
不断地提示用户输入要发送的内容,若发送的内容是”bye”则聊天结束,否则将用户输入的内容写
入到文件d:/a.txt中。
要求使用BufffferedReader类来读取键盘的输入 System.in代表键盘输入
要求使用PrintStream类负责将数据写入文件
package Task_neuedu;/*优化前*/import java.io.*;public class BufferdStreamBye {public static void main(String[] args) {BufferedReader br= null;PrintStream bw= null;try {//构造缓冲字符流对象写入br = new BufferedReader(new InputStreamReader(System.in));//构造缓冲字符流对象读取bw = new PrintStream(new FileOutputStream("d:/a2.txt"));while (true) {System.out.println("请输入内容");String str = br.readLine();if ("bye".equals(str)) { //匹配System.out.println("聊天结束!");break;}else {//否则输入到a.txt当中bw.print(str);}}} catch (IOException e) {e.printStackTrace();} finally {if (null!=br){try {br.close();} catch (IOException e) {e.printStackTrace();}}if (null!=bw){bw.close();}}}}
package Task_neuedu;//优化import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;public class BufferdStreamBye {public static void main(String[] args) {BufferedReader br= null;PrintStream bw= null;try {//构造缓冲字符流对象写入br = new BufferedReader(new InputStreamReader(System.in));//构造缓冲字符流对象读取bw = new PrintStream(new FileOutputStream("d:/a2.txt"));boolean flag=true;while (true) {System.out.println("请"+(flag?"张三":"李四")+"输入内容");String str = br.readLine();if ("bye".equals(str)) { //匹配System.out.println("聊天结束!");break;}else {//否则输入到a.txt当中Date date=new Date();SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD hh:mm:ss ");String format = sdf.format(date);bw.println(format+" "+(flag?"张三说:":"李四说:")+str);flag=!flag;}}} catch (IOException e) {e.printStackTrace();} finally {if (null!=br){try {br.close();} catch (IOException e) {e.printStackTrace();}}if (null!=bw){bw.close();}}}}
(十八) OutputStreamWriter类
(1)基本概念
java.io.OutputStreamWriter类主要用于实现从字符流到字节流的转换。
(2)常用的方法
(十九) InputStreamReader类
(1)基本概念
java.io.InputStreamReader类主要用于实现从字节流到字符流的转换。
(2)常用方法
(二十)字符编码
(1)编码表的由来
计算机只能识别二进制数据,早期就是电信号。为了方便计算机可以识别各个国家的文字,就需要
将各个国家的文字采用数字编号的方式进行描述并建立对应的关系表,该表就叫做编码表。
(2)常见的编码表
- ASCII:美国标准信息交换码, 使用一个字节的低7位二位进制进行表示。
- ISO8859-1:拉丁码表,欧洲码表,使用一个字节的8位二进制进行表示。
- GB2312:中国的中文编码表,最多使用两个字节16位二进制为进行表示。
- GBK:中国的中文编码表升级,融合了更多的中文文字符号,最多使用两个字节16位二进制位表示。
- Unicode:国际标准码,融合了目前人类使用的所有字符,为每个字符分配唯一的字符码。所有的
(3)编码的发展
面向传输的众多 UTF(UCS Transfer Format)标准出现了,UTF-8就是每次8个位传输数据,而
UTF-16就是每次16个位。这是为传输而设计的编码并使编码无国界,这样就可以显示全世界上所
有文化的字符了。
Unicode只是定义了一个庞大的、全球通用的字符集,并为每个字符规定了唯一确定的编号,具体
存储成什么样的字节流,取决于字符编码方案。推荐的Unicode编码是UTF-8和UTF-16。
UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
(二十一)DataOutputStream类(了解)
(1)基本概念
java.io.DataOutputStream类主要用于以适当的方式将基本数据类型写入输出流中。
(2)常用的方法
(二十二) DataInputStream类(了解)
(1)基本概念
java.io.DataInputStream类主要用于从输入流中读取基本数据类型的数据。
(2)常用的方法
(二十三)ObjectOutputStream类(重点)
(1)基本概念
- java.io.ObjectOutputStream类主要用于将一个对象的所有内容整体写入到输出流中。
- 只能将支持 java.io.Serializable 接口的对象写入流中。
- 类通过实现 java.io.Serializable 接口以启用其序列化功能。
- 所谓序列化主要指将一个对象需要存储的相关信息有效组织成字节序列的转化过程。 ——序列化
(2)常用的方法

基本实现 ```java package Task13;
import java.io.Serializable;
public class User implements java.io.Serializable { private static final long serialVersionUID = 5048249014317513427L; private String name; //用户名 private int Phone; //手机号 private String password; //密码
public User(String name, int phone, String password) {this.name = name;Phone = phone;this.password = password;}public User() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPhone() {return Phone;}public void setPhone(int phone) {Phone = phone;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
```javapackage Task13;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;public class ObjectOutputStreamTest {public static void main(String[] args) {ObjectOutputStream objs= null;try {objs = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));User user=new User("林一",1234567888,"nihao9090");objs.writeObject(user);System.out.println("写入成功!");} catch (IOException e) {e.printStackTrace();} finally {if (null!=objs){try {objs.close();} catch (IOException e) {e.printStackTrace();}}}}}
(二十五) ObjectInputStream类(重点)
( 1)基本概念
java.io.ObjectInputStream类主要用于从输入流中一次性将对象整体读取出来。
所谓反序列化主要指将有效组织的字节序列恢复为一个对象及相关信息的转化过程。
(2)基本方法

//方法的基本使用package Task_neuedu;import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;public class ObjectInputStreamTest {public static void main(String[] args) {ObjectInputStream objsi= null;try {//创建ObjectInputStreamTest与d:/a.txt关联,与序列化的文件对应objsi = new ObjectInputStream(new FileInputStream("d:/a.txt"));Object str= objsi.readObject();System.out.println("读取到的对象是"+str);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {if (null!=objsi) {try {objsi.close();} catch (IOException e) {e.printStackTrace();}}}}}

案例:![HJLM7SB98ZBV_`B73W]_4YN.jpg](/uploads/projects/u22174965@zew8lc/2b54e28062509d5cbcb69e447ad38478.jpeg)
package Ch12_Neuedu;import java.io.Serializable;public class product implements Serializable {private static final long serialVersionUID = 7459814710836750155L;private int id;private String name;private String categories;private double price;public product() {}public product(int id, String name, String categories, double price) {this.id = id;this.name = name;this.categories = categories;this.price = price;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCategories() {return categories;}public void setCategories(String categories) {this.categories = categories;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "product{" +"id=" + id +", name='" + name + '\'' +", categories='" + categories + '\'' +", price=" + price +'}';}}
package Ch12_Neuedu;import ch9.Array;import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;import java.io.*;import java.util.ArrayList;public class productTest {public static void main(String[] args) {ObjectOutputStream o1= null;ObjectInputStream o2= null;try {//添加产品product iphone=new product(123,"iphone","telephone",4888.0);product ipad=new product(234,"ipad","ipad",5088.0);product macbook=new product(345,"macbook","computer",10688.0);product watch=new product(345,"iwatch","watch",4799.0);//使用集合存储对象 List<product> str=new ArrayList<>();ArrayList <product> str=new ArrayList<>();//创建ObjectOutputStream与对象关联,进行序列化o1 = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));str.add(iphone);str.add(ipad);str.add(macbook);str.add(watch);//将对象序列化到d:/a.txt中o1.writeObject(str);System.out.println("写入成功!");System.out.println("反序列化中......");//创建ObjectInputStream与对象关联,进行反序列化o2 = new ObjectInputStream(new FileInputStream("d:/a.txt"));ArrayList<product> list =(ArrayList<product>) o2.readObject();System.out.println("读取到的对象是:");System.out.println("---------------------------------------------------------");for (product list2:list) {System.out.println(list2+" ");}} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try {if (o1!=null){o1.close();}} catch (IOException e) {e.printStackTrace();}try {if (o2!=null){o2.close();}} catch (IOException e) {e.printStackTrace();}}}}
#来自大佬的答案:
(1)仅供参考!

面向对象,对于对象的存储使用集合。
(3)序列化版本号
- 序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,
- JVM会把传来的字节流中的serialVersionUID与本地相应实体类的serialVersionUID进行比较,如
- 果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常
-
(4)transient关键字
transient是Java语言的关键字,用来表示一个域不是该对象串行化的一部分。当一个对象被串行
化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进
去的。
用该关键字修饰则不会再被序列化,不会写入到文件中。
(5)经验的分享
- 当希望将多个对象写入文件时,通常建议将多个对象放入一个集合中,然后将集合这个整体看做一
个对象写入输出流中,此时只需要调用一次readObject方法就可以将整个集合的数据读取出来,
从而避免了通过返回值进行是否达到文件末尾的判断。
(二十四) RandomAccessFile类
(1)基本概念
java.io.RandomAccessFile类主要支持对随机访问文件的读写操作。
(2)常用的方法
笔试考点*
(1)IO基本概念
(2) 既然有了字节流,为什么还要有字符流?
问题本质想问:不管是⽂件读写还是⽹络发送接收,信息的最⼩存储单元都是字节,那为什么
I/O 流操作要分为字节流操作和字符流操作呢?
回答:字符流是由 Java 虚拟机将字节转换得到的,问题就出在这个过程还算是⾮常耗时,并
且,如果我们不知道编码类型就很容易出现乱码问题。所以, I/O 流就⼲脆提供了⼀个直接操作
字符的接⼝,⽅便我们平时对字符进⾏流操作。如果⾳频⽂件、图⽚等媒体⽂件⽤字节流⽐
好,如果涉及到字符的话使⽤字符流⽐好。
(3)BIO\NIO\AIO的区别
">




