1. IO 表示有两个单词的缩写。
I: Input 输入 O: Output 输出
2. IO的作用:就是对文件中的内容进行操作。
输入: 读操作(读取文件的内容) 输出: 写操作(往文件中写内容)
3. IO流的分类:
(1)根据流的方向:
---输入流: 程序可以从中读取数据的流。
---输出流: 程序能向其中写入数据的流。
(2)根据流的单位:
---字节流: 以字节为单位传输数据的流
---字符流: 以字符为单位传输数据的流
(3)根据功能
---节点流: 直接和文件进行交互
---处理流: 不是直接作用在文件上。
四个基本的流: 其他的流都是在这四个流的基础上进行扩展的
字节输入流
字节输出流
字符输入流
字符输出流
它是所有字符输出流的跟类。---FileWriter类
public static void main(String[] args) throws Exception{
//字符输出流 ---指定对哪个文件(路径)进行写操作
Writer writer=new FileWriter("D:/AAA/d.txt");
String str="hello AAA 今天要开演唱会";
writer.write(str);
writer.flush(); //刷新流
writer.close(); //关闭流资源
}
上面每次往文件中写内容时 就会把原来的内容覆盖了。 如何追加内容
public static void main(String[] args) throws IOException {
//字符输出流 指定对哪个文件进行操作
//true:表示允许追加内容到文件中
Writer writer= new FileWriter("D://ABC/d.txt");
String str = "Hello 刘德华 今天来了";
writer.write(str);
//刷新流
writer.flush();
//关闭流资源
writer.close();
}
1.2 Reader字符输入流
它是所有字符输入流的根类 它的实现类有很多,我们使用FileReader实现类
public static void main(String[] args) throws IOException {
//创建字符串输入流对象 作用: 就是读取d.txt的内容
Reader reader = new FileReader("D:/ABC/d.txt");
//表示读取字符的个数
int count = 0;
//每次读取到的元素都存入该数组
char[] cs = new char[10];
while ((count=reader.read(cs))!=-1){
String str = new String(cs,0,count);
System.out.println(str);
}
1.3 完成文件的复制
@Test
public void test2() throws Exception {
//1.创建一个字的输入流
FileReader fr = new FileReader("D:/ABC/d.txt");
//2.创建一个字符输出流
FileWriter fw = new FileWriter("C:/ABC/f.txt");
//读取字符个数
int c = 0;
char[] cs = new char[10];
//每次读取内容放在该数组中
while ((c= fr.read(cs))!=-1){
fw.write(cs,0,c);
//刷新
fw.flush();
}
fw.close();
fr.close();
}
2. 字节流
2.1 字节输出流--OutputStream
它可以对任意文件进行操作,对文件进行输出操作。以字节为单位。 它是所有字节输出流的父类,FileOutputStream
//测试字节输出流
@Test
public void testOutStream() throws Exception{
//如果没有可以帮你创建,但是不能创建文件夹
OutputStream os = new FileOutputStream("D:/ABC/f.txt");
String str = "abcd";
byte[] bytes = str.getBytes();
os.write(bytes);
os.flush();
os.close();
}
2.2 字节输入流---InputStream
它可以对任意文件进行读操作 ,以字节为单位,它是所有字节输入流的父类,子类有FileInputStream
//一次一次的读取
@Test
public void testInputStream() throws Exception{
InputStream is = new FileInputStream("D:/ABC/f.txt");
byte[] bytes = new byte[3];
//一次读取3个字节并放入字节数组中 返回字节的个数
int c = is.read(bytes);
System.out.println(bytes+"==============个数"+c);
c = is.read(bytes);
System.out.println(bytes+"==============个数"+c);
c = is.read(bytes);
System.out.println(bytes+"==============个数"+c);
}
//如果文件中内容非常大 使用循环来读取
@Test
public void testInputStream2() throws Exception{
InputStream is = new FileInputStream("D:/ABC/f.txt");
byte[] bytes = new byte[300];
//读取到个数
int c =0;
while ((c=is.read(bytes))!=-1){
//把byte转为字符串
String str = new String(bytes,0,c);
System.out.println(str);
}
is.close();
}
使用字节输入和输出流完成文件的复制功能:
@Test
public void testCopy()throws Exception{
//创建字节输入流
InputStream is = new FileInputStream("D:/ABC/1.jpg");
//字节输出流
OutputStream fos = new FileOutputStream("C:/ABC/1.jpg");
byte[] bytes = new byte[10];
int c= 0;
while ((c=is.read(bytes))!=-1){
fos.write(bytes,0,c);
fos.flush();
}
is.close();
fos.close();
}
3. 缓存流
缓存流是在基础流[InputStream OutputStream Reader Writer]之上 添加了一个缓存池功能.
BufferInutStream BufferOutputStream BufferReader BufferWriter 提高IO的效率,降低IO的次数。
//测试缓存流 它是在基本流的基础上进行扩展
@Test
public void TestBuffer()throws Exception{
OutputStream out = new FileOutputStream("D:/ABC/g.txt");
//缓冲流要作用在基础流上
BufferedOutputStream bos = new BufferedOutputStream(out);
String str = "abcdefghijklmn";
byte[] bytes = str.getBytes();
//写的内容暂时放到缓存池中 并没有直接放入文件中
bos.write(bytes);
//刷新缓存池 把内容放到文件中
// bos.flush();
//关闭 先刷新缓冲池 再关闭流资源
bos.close();
}
4. 对象流
为什么需要对象流
我们现在操作IO流的时候 都是将字符串读写操作 可不可以将java对象在文件中进行读写呢? 可以的 Student st=new Student();对象
将java对象进行读写操作 意义在于持久化信息 例如: 游戏存档。
// 因为运行的时候 所有的数据都是在运行内存中的 持久化 将运行内存的数据 保存到硬盘上 存档(写) 读档(读)
public class Demo2 {
@Test
public void testObjectStream()throws Exception{
OutputStream out = new FileOutputStream("D:/ABC/a.txt");
//对象输出流
ObjectOutputStream oos = new ObjectOutputStream(out);
//使用对象输出流调用输出方法 输出的类对象 必须实现Ser
Role r = new Role("吕布","7级",1,"弑父");
oos.writeObject(r);
oos.close();
}
//测试读档--反序列化
@Test
public void testObjectStream2() throws Exception{
InputStream input = new FileInputStream("D:/ABC/a.txt");
ObjectInputStream ois = new ObjectInputStream(input);
Object o = ois.readObject();
System.out.println(o);
}
}
1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。 ---java对象所属的类必须实现序列化接口.implements Serializable 2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。5. 小结
1. 通过字符流完成文件的复制---->它只能复制文本文件
2. 字节流:---字节输入流和字节输出流。
3. 字节流 我们也可以完成文件的复制功能---它可以复制任意类型的文件.
4. 缓存流--->它基本流的基础上 添加了一个缓存池
5. 对象流: ObjectInputStream ObjectOutputStream
序列化:
反序列化:



