First read from the file and then write it into another file, which is equivalent to copying a file and the code is relatively simple.
Code:
- Package myclass. file;
- Import java. Io .*;
- Public class writetest {
- /**
- * @ Throws ioexception
- * @ Test File Reading
- */
- Public static void main (string [] ARGs) throws ioexception {
- // Write the file
- File file1 = new file ("F:/backup/ABC/B .txt ");
- Filewriter fw = new filewriter (file1 );
- Bufferedwriter BW = new bufferedwriter (FW );
- // Read the file
- File file = new file ("F:/backup/ABC/a.txt ");
- Try {
- Bufferedreader BR = new bufferedreader (New filereader (File ));
- String br1 = Br. Readline ();
- While (br1! = NULL ){
- // Method 1:
- // FW. Write (br1 + "/R/N ");
- // Method 2:
- Bw. Write (br1 );
- Bw. newline ();
- Br1 = Br. Readline ();
- }
- Bw. Close ();
- BR. Close ();
- FW. Close ();
- } Catch (exception e ){
- System. Out. println ("An error occurred while reading the file! ");
- E. printstacktrace ();
- }
- }
- }