文件输入输出流实现文件的复制

package com.xs.controller;

import java.io.*;

/**
 * @Author Administrator
 * @Date 2022/5/14 20:33
 * 文件字节输入流 将文件读取到内存中
 */
public class FileInputStreamDemo {

    public static void main(String[] args) {
//        fis();
        writerAndReader();
    }

    /**
     * 使用文件字节流读取写入文件
     */
    public static void fis(){
        // 将该文件读取入内存中
        File file = new File("G:\\Demo\\copydemo.txt");
        FileInputStream fis = null;
        FileOutputStream fio = null;
        try {
//            如果文件存在,则进行读取操作
            if (file.exists()){
                fis = new FileInputStream(file);
//                将读取的内容存放在字节数组中
                byte[] bytes = new byte[512];
//                该方法会将读取到的数据的阿斯克码存放在字节数组中
//                read方法有三个构造方法 1.read(int a) 一个字节一个字节读取
//                2.read(byte[] b) 一批一批读取 3.read(byte[] b,int off,int len) 一次读取多少个字节,从哪里开始读取,读取的长度是多少
                fis.read(bytes);
//                将读取到的内容输出:其实这里没什么意义
                for (byte b:bytes) {
                    System.out.println(b);
                }
//                将读取到的数据输出到文件中去 -1代表读取到文件末尾
                while ((fis.read(bytes)!=-1)){
//                    创建文件字节输出流 如果文件不存在,会自动创建新的文件
                    fio = new FileOutputStream("G:\\Demo\\writeTest.txt");
//                    将读取到的内容写到文件中
                    fio.write(bytes);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
//                先开后关
                fis.close();
                fio.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 使用文件字符流读取写入
     */
    public static void writerAndReader(){
        // 将该文件读取入内存中
        File file = new File("G:\\Demo\\copydemo.txt");
//        创建文件字符输入流
        FileReader reader = null;
//        创建文件字符输出流
        FileWriter writer = null;
//        创建文件字符输入流  此次需传入一个文件对象,即要读取的文件的路径
        try {
            reader = new FileReader(file);
            char[] ca = new char[512];
           reader.read(ca);
//           遍历输出:因为读取的文件是txt类型,且文件字符输入流是按字符读取的,所以此时可以将文件的内容输出到控制台,而不是阿斯克码
            for (char c:ca) {
                System.out.println(ca);
            }
//            将读到的内容写入到文件中
            writer = new FileWriter("G:\\Demo\\fileWriteTest.txt");
            String context = null;
            int len = reader.read(ca); // 读取到的有效长度
            while (len!=-1){
//                context = new String(ca);
//                writer.write(context,0, ca.length);
                for (int i = 0; i < ca.length; i++) {
                    writer.write(ca[i]);
                }
//                再次读取,注意:必须给len重新赋值,否则可能会造成死循环
                len = reader.read(ca);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值