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

}
相关推荐
忘却的纪念6 分钟前
基于SpringBoot的考研资讯平台设计与实现
java·spring boot·spring
.生产的驴7 分钟前
SpringBoot 消息队列RabbitMQ死信交换机
java·spring boot·后端·rabbitmq·java-rabbitmq
振华OPPO9 分钟前
我的5周年创作纪念日,不忘初心,方得始终。
android·java·android studio·安卓·安卓app
抚月code11 分钟前
Java线程池
java
只想摆烂@13 分钟前
C# winfrom 如何多窗体优雅的回调方法
开发语言·c#
西猫雷婶15 分钟前
python画图|中秋到了,尝试画个月亮(球体画法)
开发语言·python
IT枫斗者16 分钟前
集合工具类
java·linux·数据库·windows·算法·microsoft
星迹日17 分钟前
C语言:结构体
c语言·开发语言·经验分享·笔记
会敲代码的小张29 分钟前
设计模式-观察者模式
java·开发语言·后端·观察者模式·设计模式·代理模式
宗浩多捞33 分钟前
C++设计模式(更新中)
开发语言·c++·设计模式