IO流的使用

一、IO流的体系

二、代码应用

java 复制代码
import java.io.*;

public class Demo05 {
    public static void main(String[] args) throws IOException {
        copy1(); //1 使用原始的字节流按照一个一个字节的形式复制文件。
        copy2(); //2 使用原始的字节流按照字节数组的形式复制文件。
        copy3(); //3 使用高效的缓冲字节流按照一个一个字节的形式复制文件.
        copy4(); //4 使用高效的缓冲字节流按照字节数组的形式复制文件。

        WriteGBK(); //5 使用GBK编码进行文本写入文件
        ReadByGBK();//6 使用GBK编码读取文件

        copy6(); //7 使用字符流进行文件转存
        copy7(); //8 使用高效的缓冲字符流进行文件转存


    }

    //1 使用原始的字节流按照一个一个字节的形式复制文件。
    public static void copy1() throws IOException {
        //获取开始时间,毫秒值
        long start = System.currentTimeMillis();

        //1 创建字节输入流和输出流对象
        FileInputStream fis = new FileInputStream("要读取的文件路径");
        FileOutputStream fos = new FileOutputStream("要写入的文件路径");
        //2 循环读写,一次读写一个字节
        int by=0;
        while ((by=fis.read())!=-1){
            fos.write(by);
        }
        //3 释放资源
        fis.close();
        fos.close();

        //获取结束时间,毫秒值
        long end = System.currentTimeMillis();
        System.out.println("1 使用原始的字节流按照一个一个字节的形式复制文件 = " + (end-start));
    }


    //2 使用原始的字节流按照字节数组的形式复制文件。
    public static void copy2() throws IOException {
        //获取开始时间,毫秒值
        long start = System.currentTimeMillis();

        //1 创建字节输入流和输出流对象
        FileInputStream fis = new FileInputStream("要读取的文件路径");
        FileOutputStream fos = new FileOutputStream("要写入的文件路径");
        //2 循环读写,一次读写一个字节数组
        int len=0;
        byte[] bytes=new byte[1024];
        while ((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        //3 释放资源
        fis.close();
        fos.close();

        //获取结束时间,毫秒值
        long end = System.currentTimeMillis();
        System.out.println("2 使用原始的字节流按照字节数组的形式复制文件 = " + (end-start));
    }


    //3 使用高效的缓冲字节流按照一个一个字节的形式复制文件.
    public static void copy3() throws IOException {
        //获取开始时间,毫秒值
        long start = System.currentTimeMillis();

        //1 创建字节缓冲输入流和输出流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("要读取的文件路径"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("要写入的文件路径"));
        //2 循环读写,一次读写一个字节
        int by=0;
        while ((by=bis.read())!=-1){
            bos.write(by);
        }
        //3 释放资源
        bis.close();
        bos.close();

        //获取结束时间,毫秒值
        long end = System.currentTimeMillis();
        System.out.println("3 使用高效的缓冲字节流按照一个一个字节的形式复制文件 = " + (end-start));
    }


    //4 使用高效的缓冲字节流按照字节数组的形式复制文件。
    public static void copy4() throws IOException {
        //获取开始时间,毫秒值
        long start = System.currentTimeMillis();

        //1 创建字节缓冲输入流和输出流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("要读取的文件路径"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("要写入的文件路径"));
        //2 循环读写,一次读写一个字节数组
        int len=0;
        byte[] bytes=new byte[1024];
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        //3 释放资源
        bis.close();
        bos.close();

        //获取结束时间,毫秒值
        long end = System.currentTimeMillis();
        System.out.println("4 使用高效的缓冲字节流按照字节数组的形式复制文件 = " + (end-start));
    }

    //5 使用GBK编码进行文本写入文件
    private static void WriteGBK() throws IOException {
        //指定输出编码方式
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("要写入的文件路径"), "gbk");
        osw.write("我是GBK编码的文本");
        osw.close();
    }

    //6 使用GBK编码读取文件
    private static void ReadByGBK() throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("要读取的文件路径"), "gbk");
        char[] chars = new char[1024];
        int len;
        while ((len=inputStreamReader.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }
        inputStreamReader.close();
    }

    //7 使用字符流进行文件转存
    private static void copy6() {
        // 源文件路径
        String sourceFilePath = "要读取的文件路径";
        // 目标文件路径
        String destinationFilePath = "要写入的文件路径";

        try (FileReader fr = new FileReader(sourceFilePath);
             FileWriter fw = new FileWriter(destinationFilePath)) {

            int data;
            // 逐字符读取源文件内容并写入目标文件
            while ((data = fr.read()) != -1) {
                fw.write(data);
            }

            System.out.println("文件转存成功!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //8 使用高效的缓冲字符流进行文件转存
    private static void copy7() {
        // 源文件路径
        String sourceFilePath = "要读取的文件路径";
        // 目标文件路径
        String destinationFilePath = "要写入的文件路径";

        try (BufferedReader reader = new BufferedReader(new FileReader(sourceFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(destinationFilePath))) {

            String line;
            // 逐行读取源文件内容并写入目标文件
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine(); // 写入换行符
            }

            System.out.println("文件转存成功!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
相关推荐
rgeshfgreh2 分钟前
Spring事务传播机制深度解析
java·前端·数据库
无名-CODING2 分钟前
Java Spring 事务管理深度指南
java·数据库·spring
xiaolyuh1233 分钟前
Spring MVC Bean 参数校验 @Validated
java·spring·mvc
全栈小精灵3 分钟前
Winform入门
开发语言·机器学习·c#
心静财富之门3 分钟前
退出 for 循环,break和continue 语句
开发语言·python
txinyu的博客5 分钟前
map和unordered_map的性能对比
开发语言·数据结构·c++·算法·哈希算法·散列表
蕨蕨学AI10 分钟前
【Wolfram语言】45.2 真实数据集
java·数据库
Mr -老鬼21 分钟前
Rust适合干什么?为什么需要Rust?
开发语言·后端·rust
予枫的编程笔记24 分钟前
【Java集合】深入浅出 Java HashMap:从链表到红黑树的“进化”之路
java·开发语言·数据结构·人工智能·链表·哈希算法
ohoy30 分钟前
RedisTemplate 使用之Set
java·开发语言·redis