文件字节流、文件字符流、高效缓冲流、转换流

    public static void main(String[] args) throws IOException {
        // 文件字节流、文件字符流、文件缓冲流、转换流
        File srcFile = new File("C:\\Users\\zndx\\Desktop\\新建文件夹");
        File destFile = new File("C:\\Users\\zndx\\Desktop\\新建文件夹 (2)");
//        copyDir(srcFile, destFile);
//        charCopyFile();
        bufferCopyFile();
    }

    /**
     * 文件字节流
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFile(File srcFile, File destFile) throws IOException {
        File file = new File(destFile, srcFile.getName());
        FileInputStream fileInputStream = new FileInputStream(srcFile);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] bytes = new byte[1024];
        int len;
        while ((len = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, len);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    public static void copyDir(File srcDir, File destDir) throws IOException {
        if(!(srcDir.exists()&&srcDir.isDirectory())){
            throw new RuntimeException("源文件夹必须存在并且是一个文件夹");
        }
        if(!destDir.isDirectory()){
            throw new RuntimeException("目标文件夹必须是一个文件夹");
        }
        File file = new File(destDir, srcDir.getName());
        file.mkdirs();
        File[] files = srcDir.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isFile()) {
                    copyFile(f, file);
                }
                copyDir(f, file);
            }
        }
    }

    /**
     * 文件字符流
     * @throws IOException
     */
    public static void charCopyFile() throws IOException {
        File srcFile = new File("C:\\Users\\zndx\\Desktop\\新建文件夹\\11.txt");
        File destFile = new File("C:\\Users\\zndx\\Desktop\\新建文件夹 (2)\\22.txt");
        FileReader fileReader = new FileReader(srcFile);
        FileWriter fileWriter = new FileWriter(destFile);
        char[] chars = new char[1024];
        int len;
        while ((len = fileReader.read(chars)) != -1) {
            fileWriter.write(chars, 0, len);
            //fileWriter.flush();
        }
        fileWriter.close();
    }

    /**
     * 高效缓冲流
     * @throws IOException
     */
    public static void bufferCopyFile() throws IOException {
        //newLine   换行符
        //创建对象
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\zndx\\Desktop\\新建文件夹\\333.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\zndx\\Desktop\\新建文件夹 (2)\\444.txt"));

        //循环读写数据  把读到的数据写入目标文件中
        String line;
        while((line=br.readLine())!=null){
            //把读到的数据写入文件
            bw.write(line);
            //写入换行符
            bw.newLine();
        }

        //释放资源
        br.close();
        bw.close();
    }

    /**
     * 转换流
     * @throws IOException
     */
    public static void streamCopyFile() throws IOException {
        //字节流对象
        FileInputStream fis = new FileInputStream("C:\\Users\\zndx\\Desktop\\新建文件夹\\汉字.txt");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\zndx\\Desktop\\新建文件夹\\转换流测试.txt");
        //转换成字符流
        InputStreamReader isr = new InputStreamReader(fis);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值