Since the byte stream can copy the file, the character stream is of course also there.
the same idea :
Data Source:
A.txt--reading data--character conversion stream--InputStreamReader
Destination:
B.txt--Write data--character conversion stream--OutputStreamWriter
1 //Encapsulating Data Sources2InputStreamReader ISR =NewInputStreamReader (NewFileInputStream (3"A.txt"));4 //Package Destination5OutputStreamWriter OSW =NewOutputStreamWriter (NewFileOutputStream (6"B.txt"));7 8 //Read and write Data9 //Mode 1Ten //int ch = 0; One //While ((ch = isr.read ())! =-1) { A //osw.write (CH); - // } - the //Mode 2 - Char[] CHS =New Char[1024]; - intLen = 0; - while(len = Isr.read (CHS))! =-1) { +Osw.write (CHS, 0, Len); - //Osw.flush (); + } A at //Freeing Resources - osw.close (); -Isr.close ();
Simplified:
Most of the coding tables we use are the local default encoding tables, which are GBK. So, we can not specify the encoding table.
Also, the character stream name is a bit long ... So long, so, Java provides a subclass of the character stream for us to use:
Previously used character stream:
OutputStreamWriter = fileoutputstream + encoding table (GBK)
InputStreamReader = fileinputstream + encoding table (GBK)
To use its subclasses:
FileWriter = fileoutputstream + encoding table (GBK)
FileReader = fileinputstream + encoding table (GBK)
To redo the copy using the simplified version:
1 //Encapsulating Data Sources2FileReader FR =NewFileReader ("A.txt");3 //Package Destination4FileWriter FW =NewFileWriter ("B.txt");5 6 //copy by reading one character array at a time7 intLen = 0;8 Char[] ch =New Char[1024];9 while(len = fr.read (ch))! =-1){TenFw.write (ch,0, Len); One } A //Freeing Resources - fw.close (); - fr.close (); the}
The code is a lot less, concise and clear
File copy operations for Java 21-4 character streams and simplified