java读写数据

字节,字节数组读写

package com.test2;

import java.io.*;

public class test3 {
    public static void main(String[] args) throws IOException {
        String origin_path = "C:\\Users\\xxx\\sss\\Picture\\123.png";
        String change_path = "MyClass\\123.png";
        // record start time
        // 记录开始时间
        long startTime = System.currentTimeMillis();

        // copy video
        // 复制视频
        method4(origin_path, change_path);
//        method2();
//        method3();
//        method4();


        // record end time
        // 记录结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
    }

    // Basic byte stream reads one byte at a time
    // 基本字节流一次性读取一个字节
    public static void method1(String origin_path, String change_path) throws IOException {
        FileInputStream f1 = new FileInputStream(origin_path);
        FileOutputStream f2 = new FileOutputStream(change_path);

        int by;
        while ((by = f1.read()) != -1) {
            f2.write(by);
        }

        f1.close();
        f2.close();
    }

    //Basic byte stream reads a byte array at a time
    // 基本字节流一次性读取一个字节数组
    public static void method2(String origin_path, String change_path) throws IOException {
        FileInputStream f1 = new FileInputStream(origin_path);
        FileOutputStream f2 = new FileOutputStream(change_path);

        byte[] bys = new byte[2048];	// 注意是1024的倍数
        int len;
        while ((len = f1.read(bys)) != -1) {
            f2.write(bys, 0, len);
        }

        f1.close();
        f2.close();
    }

    // 字节缓冲流一次性读取一个字节
    public static void method3(String origin_path, String change_path) throws IOException {
        BufferedInputStream bf1 = new BufferedInputStream(new FileInputStream(origin_path));
        BufferedOutputStream bf2 = new BufferedOutputStream(new FileOutputStream(change_path));

        int by;
        while ((by = bf1.read()) != -1) {
            bf2.write(by);
        }

        bf1.close();
        bf2.close();
    }

    // 字节缓冲流一次性读取一个字节数组
    public static void method4(String origin_path, String change_path) throws IOException {
        BufferedInputStream bf1 = new BufferedInputStream(new FileInputStream(origin_path));
        BufferedOutputStream bf2 = new BufferedOutputStream(new FileOutputStream(change_path));

        byte[] bys = new byte[1024];	// 注意是1024的倍数
        int len;
        while ((len = bf1.read(bys)) != -1) {
            bf2.write(bys, 0, len);
        }

        bf1.close();
        bf2.close();
    }
}

字符,字符数组读写

package com.test2;

import java.io.*;

public class test6 {
    public static void main(String[] args) throws IOException {
        String origin_path = "C:\\Users\\herol\\Desktop\\123.txt";
        String change_path = "MyClass\\幻想爱情故事.txt";
        // record start time
        // 记录开始时间
        long startTime = System.currentTimeMillis();

        // copy video
        // 复制视频
        method5(origin_path, change_path);
//        method2();
//        method3();
//        method4();


        // record end time
        // 记录结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
    }

    // Basic byte stream reads one byte at a time
    // 基本字符流一次性读取一个字符
    public static void method1(String origin_path, String change_path) throws IOException {
        /**
         * InputStreamReader f1 = new InputStreamReader(new FileReader(origin_path));
         * 将这部分的输入流替换为下面的FileReader更为简洁,但是如果需要更改编码的话还是需要使用这个复杂的
         */
        FileReader f1 = new FileReader(origin_path);
        FileWriter f2 = new FileWriter(change_path);

        int by;
        while ((by = f1.read()) != -1) {
            f2.write(by);
        }

        f1.close();
        f2.close();
    }

    //Basic byte stream reads a byte array at a time
    // 基本字节流一次性读取一个字节数组
    public static void method2(String origin_path, String change_path) throws IOException {
        FileReader f1 = new FileReader(origin_path);
        FileWriter f2 = new FileWriter(change_path);

        /**
         * 注意这是字符数组,不是字节数组
         */
        char[] bys = new char[2048];	// 注意是1024的倍数
        int len;
        while ((len = f1.read(bys)) != -1) {
            f2.write(bys, 0, len);
        }

        f1.close();
        f2.close();
    }

    // 字符缓冲流一次性读取一个字节
    public static void method3(String origin_path, String change_path) throws IOException {
        BufferedReader bf1 = new BufferedReader(new FileReader(origin_path));
        BufferedWriter bf2 = new BufferedWriter(new FileWriter(change_path));

        int by;
        while ((by = bf1.read()) != -1) {
            bf2.write(by);
        }

        bf1.close();
        bf2.close();
    }

    // 字符缓冲流一次性读取一个字节数组
    public static void method4(String origin_path, String change_path) throws IOException {
        BufferedReader bf1 = new BufferedReader(new FileReader(origin_path));
        BufferedWriter bf2 = new BufferedWriter(new FileWriter(change_path));

        char[] bys = new char[1024];	// 注意是1024的倍数
        int len;
        while ((len = bf1.read(bys)) != -1) {
            bf2.write(bys, 0, len);
        }

        bf1.close();
        bf2.close();
    }

    // 字符缓冲流特有方法:换行
    public static void method5(String origin_path, String change_path) throws IOException {
        BufferedReader bf1 = new BufferedReader(new FileReader(origin_path));
        BufferedWriter bf2 = new BufferedWriter(new FileWriter(change_path));

        String line;
        while ((line = bf1.readLine()) != null) { //读到换行符则显示为空
            bf2.write(line);   
            bf2.newLine();  // 进行换行
            bf2.flush();    // 最好进行刷新
        }

        bf1.close();
        bf2.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值