File copy operations for Java 21-4 character streams and simplified

Source: Internet
Author: User
Tags file copy

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.