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

}
相关推荐
万粉变现经纪人31 分钟前
如何解决 pip install -r requirements.txt 私有索引未设为 trusted-host 导致拒绝 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
qq_4798754335 分钟前
C++ std::Set<std::pair>
开发语言·c++
毕业设计制作和分享37 分钟前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
云知谷3 小时前
【C++基本功】C++适合做什么,哪些领域适合哪些领域不适合?
c语言·开发语言·c++·人工智能·团队开发
l1t4 小时前
DeepSeek辅助利用搬移底层xml实现快速编辑xlsx文件的python程序
xml·开发语言·python·xlsx
不吃凉粉4 小时前
Android Studio USB串口通信
android·ide·android studio
zhangphil5 小时前
android studio设置大内存,提升编译速度
android·android studio
C_Liu_6 小时前
C++:list
开发语言·c++
my rainy days6 小时前
C++:友元
开发语言·c++·算法
编程乐学6 小时前
安卓非原创--基于Android Studio 实现的天气预报App
android·ide·android studio·课程设计·大作业·天气预报·安卓大作业