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

}
相关推荐
孟陬1 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌2 小时前
一站式了解四种限流算法
java·后端·go
阿巴斯甜2 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
华仔啊2 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
Kapaseker2 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
也些宝3 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
xq95273 小时前
Andorid Google 登录接入文档
android
Nyarlathotep01133 小时前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen53 小时前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java