IO流

目录

1.IO流简介

 2.Writer字符输出流

3.Reader字符输入流

3.1 完成文件的复制

4.字节流

4.1 字节输出流--OutputStream

4.2 字节输入流---InputStream

5.缓存流

6.对象流--对java对象进行IO操作

总结:

 


1.IO流简介

1. IO 表示有两个单词的缩写。
  I: Input 输入   O: Output 输出

2. IO的作用:就是对文件中的内容进行操作。
  输入: 读操作(读取文件的内容)   输出: 写操作(往文件中写内容)
 
3. IO流的分类:

 (1)根据流的方向:
    ---输入流: 程序可以从中读取数据的流。
    ---输出流: 程序能向其中写入数据的流。
    
 (2)根据流的单位:
    ---字节流: 以字节为单位传输数据的流
    ---字符流: 以字符为单位传输数据的流
 
 (3)根据功能
    ---节点流: 直接和文件进行交互
    ---处理流: 不是直接作用在文件上。
    
四个基本的流: 其他的流都是在这四个流的基础上进行扩展的
        字节输入流
        字节输出流
    
        字符输入流
        字符输出流

IO: 就是通过IO流对文件的内容进行操作。   
      IO按照方向: 输入流和输出流
      IO按照内容: 字节流和字符流。
      IO功能: 节点流和处理流
      
4.IO中的4个基类:
      InputStream:字节输入流
      OutputStream:字节输出流
      Reader: 字符输入流
      Writer: 字符输出流

5. 字符输出和输入流

 

 2.Writer字符输出流

FileWriter类:它是所有字符输出流的根类。

class TestWriter{
    public static void main(String[] args) throws Exception{
        //字符输出流,指定文件(路径)进行写操作
        FileWriter w1 = new FileWriter("D:/AAA/d.txt",true);
        //true:表示允许追加内容到文件中
        //添加内容
        w1.write("大师兄,师傅被妖怪抓走了");
        //刷新流
        w1.flush();
        //关闭流
        w1.close();
    }
}

上面每次往文件中写内容时 就会把原来的内容覆盖了。 如何追加内容 :

class TestWriter2{
    public static void main(String[] args) throws Exception{
        //字符输出流,指定文件(路径)进行写操作
        FileWriter w1 = new FileWriter("D:/AAA/d.txt",true);
        //true:表示允许追加内容到文件中
        //添加内容
        w1.write("二师兄,师傅被妖怪抓走了");
        //刷新流
        w1.flush();
        //关闭流
        w1.close();
    }
}

3.Reader字符输入流

它是所有字符输入流的根类  它的实现类有很多,我们使用FileReader实现类。

class TestReader{
        public static void main(String[] args) throws Exception{
            //创建字符输入流对象, 作用:读取文件内容
            FileReader r1 = new FileReader("D:/AAA/f.txt");
            //while循环输出内容(效率慢,每次读取一个字符)
            int i=0;
            while ((i=r1.read())!=-1){
                System.out.print((char) i);
            }
        }
    }

    class TestRearder2{
        public static void main(String[] args) throws Exception {
            //创建字符输入流对象 读取文件内容
            FileReader f1 = new FileReader("D:/AAA/f.txt");
            //表示读取的字符个数
            int count=0;
            //把每次读到的数据存入数组
            char[] cs=new char[5];
            //while循环输出内容
            while ((count=f1.read(cs))!=-1){
                String str=new String(cs,0,count);
                System.out.print(str);
            }
        }
    }

3.1 完成文件的复制

public class Copy {
    @Test
    public void CopyFile()throws Exception{
        //创建字符输入流
        FileReader file1 = new FileReader("D:/AAA/b.txt");
        //创建字符输出流
        FileWriter file2 = new FileWriter("D:/BBB/e.txt");
        //记录读取到的字符个数
        int count=0;
        //创建数组用来储存读取到的内容
        char[] chars = new char[10];
        //while 循环写入内容
        while ((count=file1.read(chars))!=-1){
            //把数组内容写入指定文件
            file2.write(chars,0,count);
            //刷新文件
            file2.flush();
        }
        //关闭流
        file1.close();
        file2.close();
    }

}

  只能对文本进行操作。 因为视频图片 这些都属于二进制。

4.字节流

4.1 字节输出流--OutputStream

可以对任意文件进行操作,对文件进行输出操作。以字节为单位。 它是所有字节输出流的父类,
FileOutputStream.

public class TestDemo1 {
     //测试字节输出流
    @Test
    public void tsetOutputStream()throws Exception{
     //创建自己输出流
     FileOutputStream f1 = new FileOutputStream("D:/AAA/cc.txt");
     //定义添加的内容
     String str="123456789";
     //把字符内容转化为byte数组
     byte[] bytes=str.getBytes();
     //把字节数组内容添加到文件中
     f1.write(bytes);
     //刷新流
     f1.flush();
     //关闭流
     f1.close();
    }

4.2 字节输入流---InputStream

可以对任意文件进行读操作 ,以字节为单位,它是所有字节输入流的父类,子类有FileInputStream

单次次重复读取(不推荐):

//创建字节输入流
    @Test
    public void testInputStream()throws Exception{
        //创建字节输入流
        FileInputStream f1 = new FileInputStream("D:/AAA/bb.txt");
        //创建字节数组储存读取的数据
        byte[] bytes=new byte[3];
        //多次读取数据内容
        //读取的字节个数
        int num=f1.read(bytes);
        System.out.println(bytes+"读取的字节数:"+num);
        //再次读取
        num=f1.read(bytes);
        System.out.println(bytes+"读取的字节数:"+num);
        //再次读取
        num=f1.read(bytes);
        System.out.println(bytes+"读取的字节数:"+num);
        //再次读取
        num=f1.read(bytes);
        System.out.println(bytes+"读取的字节数:"+num);
    }

循环读取:

//文件过大重复读取字节数据浪费时间可以使用while循环获取数据
    @Test
    public void tsetInputStream2()throws Exception{
        //创建字节流
        FileInputStream f1 = new FileInputStream("D:/AAA/cc.txt");
        //获取字节的个数
        int num=0;
        //创建数组用来储存字节文件
        byte[] bytes=new byte[3];
        //while循环读取内容
        while ((num=f1.read(bytes))!=-1){
            //获取的数据转化为字符串
            String str=new String(bytes);
            //输出读取的内容
            System.out.print(str);
        }
        //关闭流
        f1.close();
    }

使用字节流复制文件或图片:

 //使用字节输入、输出复制图片或视频
    @Test
    public void copy()throws Exception{
        //创建一个输入流
        FileInputStream p1 = new FileInputStream("D:/AAA/1.png");
        //创建一个输出流
        FileOutputStream p2 = new FileOutputStream("D:/BBB/1.png");
        //读取字节的个数
        int num=0;
        //创建byte数组储存读取内容
        byte[] bytes=new byte[5];
        //while循环读取并写入数据
        while ((num=p1.read(bytes))!=-1){
            //写入指定文件
            p2.write(bytes,0,num);
        }
        //关闭流
        p2.close();
        p1.close();
    }

5.缓存流

        缓存流是在基础流[InputStream OutputStream Reader Writer]之上 添加了一个缓存池功能.
BufferInutStream  BufferOutputStream BufferReader  BufferWriter 提高IO的效率,降低IO的次数。

//缓存流
    @Test
    public void testBuffer()throws Exception{
        //创建输出流
        FileOutputStream f1 = new FileOutputStream("D:/AAA/f.txt");
        //创建缓存池(缓存流要作用在基础流上)
        BufferedOutputStream b2 = new BufferedOutputStream(f1);
        //定义储存数据
        String str="ABCDFEG";
        //把储存数据转换为字节数组
        byte[] bytes=str.getBytes();
        //写入的内容暂时储存在缓存池中,所以并不在文件内显示出来
        b2.write(bytes);
        //关闭流(先进行缓冲池刷新后关闭流)
        b2.close();
    }
}

6.对象流--对java对象进行IO操作

为什么需要对象流
  我们现在操作IO流的时候 都是将字符串读写操作 可不可以将java对象在文件中进行读写呢? 可以的 Student st=new Student();对象
  将java对象进行读写操作 意义在于持久化信息  例如: 游戏存档。
 
   //  因为运行的时候 所有的数据都是在运行内存中的   持久化 将运行内存的数据 保存到硬盘上    存档(写)  读档(读)

对象流的序列化与反序列化:

package com.new151.Test4_20;

import org.junit.Test;

import java.io.*;

/**
 * @Description:
 * @Author:LouFangRui
 * @Date:2022/4/21
 */
public class TestDemo2 {
	//存档--可序列化
	@Test
	public void testObjectStream()throws Exception{
		//创建字节输出流
		FileOutputStream f1 = new FileOutputStream("D:/AAA/l.txt");
		//创建对象输出流
		ObjectOutputStream b1 = new ObjectOutputStream(f1);
		//使用对象输出流调用方法,输出类的对象实现Serializable接口(可序列化)
		hero hero = new hero("蔡文姬", "9级", 7, "大帽");
		//把内容写入文件
		b1.writeObject(hero);
		//关闭流
		b1.close();
	}
	//读档--反序列化
	@Test
	public void testObjectStream2()throws Exception{
		//创建字节输入流
		FileInputStream f2 = new FileInputStream("D:/AAA/l.txt");
		//创建对象输入流
		ObjectInputStream b2 = new ObjectInputStream(f2);
		//读取内容
		Object a=b2.readObject();
		//输出a
		System.out.println(a);
		//关闭流
		b2.close();
	}
}
	//创建英雄类实现Serializable接口
	class hero implements Serializable{
		//属性私有化
		private String name;
		private String level;
		private int id;
		private String wuqi;

		//构造方法
		public hero() {
		}

		public hero(String name, String level, int id, String wuqi) {
			this.name = name;
			this.level = level;
			this.id = id;
			this.wuqi = wuqi;
		}
		//getter、setter方法
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getLevel() {
			return level;
		}

		public void setLevel(String level) {
			this.level = level;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getWuqi() {
			return wuqi;
		}

		public void setWuqi(String wuqi) {
			this.wuqi = wuqi;
		}
		//toSreing方法

		@Override
		public String toString() {
			return "hero{" +
					"name='" + name + '\'' +
					", level='" + level + '\'' +
					", id=" + id +
					", wuqi='" + wuqi + '\'' +
					'}';
		}
	}

 1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。
         ---java对象所属的类必须实现序列化接口.implements Serializable
2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。

总结:

1.  通过字符流完成文件的复制---->它只能复制文本文件
2.  字节流:---字节输入流和字节输出流。
3.  字节流 我们也可以完成文件的复制功能---它可以复制任意类型的文件.
4.  缓存流--->它基本流的基础上 添加了一个缓存池
5.  对象流: ObjectInputStream  ObjectOutputStream
     序列化:把内存中的java对象存储到磁盘[网盘]的过程。
         ---java对象所属的类必须实现序列化接口.implements Serializable
     反序列化:把磁盘中的内容读取到java对象内存中的过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值