IO流读取本机文件和复制文件

该文章展示了如何在Java中使用字符流和字节流读取、复制文件。示例代码包括用字符流读取文件并打印,用字节流复制文件,以及使用包装流(如BufferedReader和BufferedWriter)来处理文本文件的复制。所有方法都处理了可能出现的FileNotFoundException和IOException。
摘要由CSDN通过智能技术生成

用字符流读取文件并打印

//文件读取(字符流)
public static void readStream() {
    String path = "G:\\wjfz.txt";//文件位置
    File file = new File(path);
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[100];
        int num = 0;
        while ((num = fileInputStream.read(bytes)) != -1) {
            String s = new String(bytes, 0, num);
            System.out.println(s);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

用字符流复制文件

//文件复制(字符流)
public static void copyStream() {
    String path = "C:\\Users\\xlz\\Desktop\\71ac2d26-45042d7.png";//文件位置
    String copyPath = "G:\\io\\";//复制到文件位置
    String endName = path.substring(path.indexOf("."));//截取文件后缀名
    String copyName = copyPath + UUID.randomUUID() + endName;//生成新的文件名称
    try {
        FileInputStream fileInputStream = new FileInputStream(path);
        FileOutputStream fileOutputStream = new FileOutputStream(copyName);
        int num = 0;
        byte[] bytes = new byte[100];
        while ((num = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, num);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

用字符流文件复制(只能复制文本文件)

//文件复制(字节流)只能读取text文本文件
public static void copyReader(){
    String path = "G:\\io\\111.txt";
    String copyPath = "G:\\io\\";
    String name = copyPath+UUID.randomUUID()+".txt";
    System.out.println(name);
    try {
        FileReader fileReader = new FileReader(path);
        FileWriter fileWriter = new FileWriter(name);
        int num = 0;
        char[] chars = new char[10];
        while ((num = fileReader.read(chars))!=-1){
            fileWriter.write(chars,0,num);
        }
        fileWriter.flush();
        fileReader.close();
        fileWriter.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

文件复制用包装流进行包装复制(只能复制文本文件)

//文件复制用Stream和reader包装
public static void copyStreamReader(){
    String path = "G:\\io\\111.txt";//文件位置
    String endName = path.substring(path.indexOf("."));//文件名后缀
    String copyPath = "G:\\io\\";//复制到哪里
    String name = copyPath + UUID.randomUUID()+endName;//生成新的文件名
    try {
        FileInputStream fileInputStream = new FileInputStream(path);//字节流
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);//字节流转成字符流
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);//字符流
        FileOutputStream fileOutputStream = new FileOutputStream(name);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        String s = null;
        while ((s = bufferedReader.readLine())!=null){
            bufferedWriter.write(s+"\n");
        }
        bufferedWriter.flush();
        bufferedWriter.close();
        bufferedReader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值