Java FileReader and Filewriter__java

Source: Internet
Author: User
Java FileReader and FileWriter Overview

Character streams are IO streams that can read characters directly

Character stream to read the characters, it is necessary to read the byte data first, then convert to the character. If you want to write a character, you need to convert the character to byte and write

Whether a character stream can copy files that are not plain text. Files that are not text-less can be copied because the bytes are converted to characters when they are read, and the corresponding characters may not be found in the conversion process. Instead, write the characters into bytes to write out, if it is?, write directly, so that the file after writing a mess, can not see the copy file example

filereader reader = null;

FileWriter writer = null;
    try{//Create input/output stream reader = new FileReader ("E:/readme.txt");

    writer = new FileWriter ("D:/temp.txt");
    char[] buffer = new char[1024 * 8];

    int Len;
    Read and write data while (len = reader.read (buffer) > 0) {writer.write (buffer, 0, Len);
} System.out.println ("done."); }catch (IOException e) {e.printstacktrace ();}
        finally {try{//close the input stream if (reader!= null) {reader.close ();
    }}catch (IOException e) {e.printstacktrace ();
            }finally {try {//Turn off output stream if (writer!= null) {writer.close ();
        } catch (IOException e) {e.printstacktrace (); }
    }
}

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.