Java中的IO流

IO流概念:

以后会遇到 上传和下载 等这些需求。
I input 输入
O output 输出
咱们电脑上面文件,在进行读取和存储的时候,都是以流的形式进行操作的
流这个概念是比较抽象的

缓冲的概念:

看视频有点卡? 暂停的时候在缓冲的
缓冲其实就是为了提高读取和存储的效率的
计算机通过cpu读取硬盘的数据,在Java中可以加上缓冲的概念,每次读取具体的缓冲值。可以提高效率

IO流

从磁盘(c盘)读取数据到内存(Java代码) 1.txt====》java代码(输出出来)
输入流:
​ input :场景使用 从磁盘的c:/aaa/1.txt文件中 读取数据 到内存中(Java代码)
​ 字节输入流
​ 字符输入流
从内存(Java代码 String=“狗蛋”)写入数据到 磁盘(c盘 1.txt)
输出流:
​ output : 从内存(String str = " 还是数据库的借口借口") 写入到磁盘的c:/aaa/1.txt文件中
​ 字节输出流:
​ 字符输出流
参照物体 是内存

字节输入流:

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();


    }
}

字节输出流:

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();

    }

}

字节输入流:

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();
    }
}

字节输出流:

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();
    }
}

复制一个文本文档到另外一个盘符下面:

package com.qf.f_zonghe;

import java.io.*;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        //字符流将一个文本赋值2到另外一个文件夹下面
        //BufferedReader  专门将文本内容读取到内存中
        BufferedReader br = new BufferedReader(new FileReader("c:/bbb/《雪中悍刀行》.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("c:/aaa/88.txt"));
        char[] cbuf= new char[1024];
        int length;
        while ((length = br.read(cbuf)) != -1) {
            bw.write(cbuf, 0, length);
        }
        bw.close();
        br.close();
    }
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值