Java-IO流

IO流

上传和下载 等这些需求。

I  input  输入

O  output  输出

咱们电脑上面文件,在进行读取和存储的时候,都是以流的形式进行操作的

流这个概念是比较抽象的

 1.1缓冲的概念

看视频有点卡? 暂停的时候在缓冲的

缓冲其实就是为了提高读取和存储的效率的

计算机通过cpu读取硬盘的数据,在Java中可以加上缓冲的概念,每次读取具体的缓冲值。可以提高效

 1.2IO流

从磁盘(c盘)读取数据到内存(Java代码)    1.txt====》java代码(输出出来)     

输入流:

        input   :场景使用  从磁盘的c:/aaa/1.txt文件中 读取数据 到内存中(Java代码)

        字节输入流

        字符输入流

从内存(Java代码  String="狗蛋")写入数据到 磁盘(c盘 1.txt)  

输出流:

        output  :  从内存(String str = " 还是数据库的借口借口") 写入到磁盘的c:/aaa/1.txt文件中

        字节输出流:

        字符输出流

参照物体 是内存

1.2.1字节输入流

FileInputStream:将磁盘上面文件的内容读取到Java代码中

        

package com.qf.a_fileinputstream;

import java.io.*;


public class Demo2 {
    public static void main(String[] args) throws IOException {
        //1.创建一个file对象的  文件抽象
        File file = new File("c:/aaa/1.txt");
        //2.创建字节输入流的核心的类
        FileInputStream fis = new FileInputStream(file);
        //3.加缓冲功能
        BufferedInputStream bis = new BufferedInputStream(fis);
        //4.创建一个数组  缓冲数组
        byte[] buf = new byte[4 * 1024];//4096字节

        /**
         * 5.读取方法
         * public int read(byte[] b)   将文件中的数据 读取到缓冲数组中
         *          throws IOException
         * 从该输入流读取最多b.length字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。
         * 重写:
         * read在 InputStream类
         * 参数
         * b - 读取数据的缓冲区。
         * 结果
         * 读入缓冲区的总字节数,如果没有更多的数据,因为文件的结尾已经到达, -1 。
         */
        int length;
        while ((length = bis.read(buf)) != -1) {
            System.out.println(new String(buf, 0, length));
        }
        //6.关闭
        bis.close();
        fis.close();


    }
}

1.2.2字节输出流

FileOutputStream:将Java代码中的一个字符串 写入到磁盘中的一个2.txt

package com.qf.b_fileoutputstream;

import java.io.*;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        //1.创建文件对象
        File file = new File("c:/aaa/2.txt");
        //2.创建核心的字节输出流对象FileOutputStream
        FileOutputStream fos = new FileOutputStream(file);
        //3.加缓冲的效果  BufferedOutputStream
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //4.声明一个字符串
        String str = "abcdefgh";
        /**
         * void	write(byte[] b)  将字节数组中的数据写入到
         * 将 b.length个字节从指定的字节数组写入此文件输出流。
         */
        //bos.write(str.getBytes());
        /**
         * void	write(byte[] b, int off, int len)
         * 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
         */
        //bos.write(str.getBytes(), 0, 3);
        bos.write(1);
        bos.close();
        fos.close();

    }
}

针对于上面讲的写一个案例:

​    c:/bbb下面有一个视频文件  复制到c:/aaa下面

使用Java代码 

思路:  先从磁盘读取到内存缓冲数组(输入流)中然后再写入磁盘中(输出)

package com.qf.c_zonghe;

import java.io.*;
public class Demo1 {
    public static void main(String[] args) throws IOException {
        copyVideo1();
    }
    public static void copyVideo () throws IOException {
        //创建缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("c:/bbb/12转义字符.mp4")));
        //创建缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("c:/aaa/sb.mp4")));
        //准备一个缓冲数组
        byte[] buf = new byte[4096];
        int length;
        while ((length = bis.read(buf)) != -1) {//length = bis.read(buf)  从磁盘读取数据到缓冲数组中
            bos.write(buf, 0, length);//从缓冲数组中写入到磁盘
        }
        bos.close();
        bis.close();
    }
    //不带缓冲的
    public static void copyVideo1 () throws IOException {
       FileInputStream fis = new FileInputStream(new File("c:/bbb/12转义字符.mp4"));
       FileOutputStream fos = new FileOutputStream(new File("c:/aaa/2b.mp4"));
       int length;
       while ((length = fis.read()) != -1) {
            fos.write(length);
       }
       fos.close();
       fis.close();

    }

}

1.2.3字符输入流【非重点】

FileReader

将磁盘中的文件的内容读取到Java代码中

阅读字符文件的便利类

`FileReader`是用于读取字符流。 要读取原始字节流(图片 音频 视频),请考虑使用`FileInputStream` 

FileReader是从字节流到字符流的桥:它读取字节,并使用指定的`charset`将其解码为[字符]

package com.qf.d_filereader;

import java.io.*;


public class Demo1 {
    public static void main(String[] args) throws IOException {
        //1.创建file对象
        File file  = new File("c:/aaa/1.txt");
        //2.创建字符输入流的核心的对象
        FileReader fr = new FileReader(file);
        //3.加缓冲效果
        BufferedReader br = new BufferedReader(fr);
        //4.准备缓冲的数组
        char[] cbuf = new char[2];
        //从磁盘读取数据到缓冲数组中
        int length;
        while ((length = br.read(cbuf)) != -1) {
            System.out.println(new String(cbuf, 0, length));
        }
        br.close();
        fr.close();
    }
}

 1.2.4字符输出流【非重点】

FileWriter

场景:   Java中有一个字符串然后写入到磁盘的一个文件中

package com.qf.e_filewriter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        //File file = new File();
        FileWriter fw = new FileWriter("c:/aaa/2.txt", true);
        BufferedWriter bw = new BufferedWriter(fw);
        String str = "bhxsanxn";
        //write
        //bw.write(str);
        //void	write(String str)
        //写一个字符串
//        void	write(String str, int off, int len)
//        写一个字符串的一部分。
        //bw.write(str, 3, 2);
        //void	write(int c)
        //写一个字符
        //bw.write(97);
//        void	write(char[] cbuf)
//        写入一个字符数组。
        bw.write(str.toCharArray());
        //abstract void	write(char[] cbuf, int off, int len)
        //写入字符数组的一部分。
        //bw.write(str.toCharArray(), 3, 1);
//        bw.write("狗蛋");
//        bw.newLine();
//        bw.write("毛蛋");
//        bw.newLine();
//        bw.write("老邢");
        bw.close();
        fw.close();
    }
}

总结:
    1.输入流和输出流的功能
        输入流:  从磁盘到内存(Java代码)
        输出流:  从内存(java 代码)到磁盘
    2.输入流
        分为两种:
            字节输入流
                FileInputSrteam 
            字符输入流
                FileReader
                核心方法:  read
                
            关于效率问题,他们两个有对应的缓冲流
                FileInputSrteam 对应的BufferedInputStream
                FileReader 对应的 BufferedReader
    3.输出流
        分为两种:
            字节输出流:
                FileOutputStream
            字符输出流:
                FileWriter
                    核心的方法 write
            关于效率问题,他们两个有对应的缓冲流
            FileOutputStream对应的缓冲流 BufferedOutputStream 
            FileWriter对应缓冲流   BufferedWriter
    4.记住一句话 用字节流    

2.序列化和反序列化

新建类然后创建对象,然后对对象的二属性赋值。对象真实存在了,然后可以通过流来将对象存到本地磁盘上面,这就叫序列化。  本次磁盘上面存的有对象的数据,咱们可以再通过流读取到Java代码中变成对象

这叫反序列化   持久性操作

 从内存到磁盘  序列化       输出流   ObjectOutputStream           write

package com.qf.g_serialize;

import java.io.*;
import java.util.ArrayList;


class Employee implements Serializable {
    String name;
    int age;
    transient int ID;//短暂的  此属性不可序列化
    String adress;//地址

    public void eat() {
        System.out.println("今天中午没有吃饱");
    }
}

public class Demo1 {
    public static void main(String[] args) throws IOException {
        ArrayList arrayList = new ArrayList();
        Employee employee = new Employee();
        employee.name = "gousheng";
        employee.adress = "航海中路";
        employee.age = 18;
        employee.ID = 8989;
        //将Java代码中的对象存到磁盘中
        FileOutputStream fos = new FileOutputStream("c:/aaa/emp.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(employee);
        oos.close();
        fos.close();

    }
}

反序列化 从磁盘到 内存中 inputStream

package com.qf.g_serialize;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;


public class Demo2 {
    public static void main(String[] args) throws Exception {
        //反序列化
        FileInputStream fis = new FileInputStream("c:/aaa/emp.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Employee emp = (Employee)ois.readObject();
        System.out.println(emp.name);
        System.out.println(emp.age);
        System.out.println(emp.adress);
        System.out.println(emp.ID);//0

    }
}

注意事项:
请注意,一个类的对象要想序列化成功,必须满足两个条件:

该类必须实现 java.io.Serializable 接口。

该类的所有属性必须是可序列化的。如果有一个属性不是可序列化的,则该属性必须注明是短暂的

 io流
    输入流 :  从磁盘读取数据到内存(Java代码)
        核心类:
            字节输入流  FileInputStream
            字符输入流  FileReader
            对象输入流  ObjectInputStream
    输出流 :  从内存到磁盘
            字节输出流:   FileOutputStream
            字符输出流:   FileWriter
            对象输出流:   ObjectOutputStream

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值