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

}
相关推荐
zhousenshan2 分钟前
Python爬虫常用框架
开发语言·爬虫·python
皮皮林55140 分钟前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_456904271 小时前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_1 小时前
异步编程CompletionService
java
DKPT1 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
sibylyue1 小时前
Guava中常用的工具类
java·guava
奔跑吧邓邓子1 小时前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
专注API从业者1 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
一笑的小酒馆2 小时前
Android性能优化之截屏时黑屏卡顿问题
android
奔跑吧邓邓子2 小时前
【Java实战㉝】Spring Boot实战:从入门到自动配置的进阶之路
java·spring boot·实战·自动配置