java buffer copy text file

public class CopyTextByBuf {
    public static void main(String[] args) {
        BufferedReader bufr = null;
        BufferedWriter bufw = null;
        try {
            bufr = new BufferedReader(new FileReader("demo_src.txt"));
            bufw = new BufferedWriter(new FileWriter("demo_desc.txt"));
            String line = null ;
             // readLine without line terminator 
            while ((line = bufr.readLine ())! = Null ) {
                bufw.write(line);
                bufw.newLine();
                bufw.flush();
            }
        } catch (IOException e) {
             throw  new RuntimeException ("Read and write failed!" );
        } finally {
            try {
                if (bufr != null)
                    bufr.close();
            } catch (IOException e) {
                 throw  new RuntimeException ("Read close failed!" );
            }
            try {
                if (bufw != null)
                    bufw.close();
            } catch (IOException e) {
                 throw  new RuntimeException ("write close failed!" );
            }
        }
    }
}

Whether reading a line or getting multiple characters, newLine () actually reads one by one on the hard disk. So in the end the read () method is used one at a time.


Guess you like

Origin www.cnblogs.com/hongxiao2020/p/12677107.html