• Java之字符流操作-复制文件


     1 package test_demo.fileoper;
     2 
     3 import java.io.*;
     4 
     5 /*
     6 * 字符输入输出流操作,复制文件
     7 * 使用缓冲流扩展,逐行复制
     8 * */
     9 public class FileReadWriteOper {
    10     public static void main(String args[]) {
    11         //字符流输入流
    12         FileReader fr = null;
    13         //字符流输出流
    14         FileWriter fw = null;
    15         //缓冲输入流
    16         BufferedReader br = null;
    17         //缓冲输出流
    18         BufferedWriter bw = null;
    19 
    20         try {
    21             fr = new FileReader(new File("C:\testdata\filedir\a.txt"));
    22             br = new BufferedReader(fr);   //扩容,类似加水管
    23             fw = new FileWriter("C:\testdata\filedir\c.txt");
    24             bw = new BufferedWriter(fw);
    25             //逐行复制
    26             String line = br.readLine();
    27             while (line != null) {
    28                 bw.write(line);
    29                 bw.newLine();  //换行输出
    30                 line = br.readLine();
    31             }
    32             System.out.println("文件复制成功!");
    33         } catch (IOException e) {
    34             e.printStackTrace();
    35         } finally {
    36             try {
    37                 //关闭流,顺序与打开相反
    38                 bw.close();
    39                 br.close();
    40                 fw.close();
    41                 fr.close();
    42             } catch (IOException e) {
    43                 e.printStackTrace();
    44             }
    45         }
    46     }
    47 }
  • 相关阅读:
    python中的面向对象编程
    python global vs nonlocal (2)
    python3 nonlocal vs global
    poj蚂蚁问题
    C/C++ static vs global
    砝码问题
    Wythoff's game
    C++中的::operator new, ::operator delete
    客户信息表 自我汇总 待确认
    Oracle Savepoint
  • 原文地址:https://www.cnblogs.com/gongxr/p/7992405.html
Copyright © 2020-2023  润新知