Java FileWriter class and FileReader class

Source: Internet
Author: User

I. FileReader class
1, construction method:
FileReader fr = new FileReader (String fileName);//Use the construction method of the string parameter with the specified file. Creates the input stream object. and associate the source file.
2, the Main method:
int read (); Reads a single character, returns an int variable that represents the read character, and returns 1 if the end of the stream has been reached.
int read (char []cbuf);//The character is read into the array. Returns the number of characters read. Returns 1 if the trailer has reached the end.
void close ();//close this stream object. Frees all resources associated with it.


example,
3.FileReader Read File contents
① Method One:
try{
The FileReader class inherits from the InputStreamReader class. This class reads the data in the stream by character.
FileReader inread=new FileReader ("E:\\xxx\\1\\beautifulsoup summary. txt");
Read stream of characters
BufferedReader in=new BufferedReader (inread);
BufferedReader in=new BufferedReader (New FileReader ("E:\\xxx\\test.txt"));
String str;//to hold the contents of the Read file
while ((Str=in.readline ())!=null) {
System.out.println (str);
}
System.out.println (str);
}catch (Exception e) {
E.printstacktrace ();
}
② Method Two
try{
FileReader read=new FileReader ("E:\\xxx\\test.txt");
int ch = 0;
Read () reads a single character, returning an int variable that represents the read-to character
while ((ch = read.read ())! =-1) {
System.out.print ((char) ch);
}
Read.close ();
}catch (Exception e) {
E.printstacktrace ();
}

Two, FileWriter class (character output stream class)

1, construction method:

FileWriter fw = new FileWriter (String fileName); Creates a character output stream class object associated with a file that already exists. The file does not exist and is created.
such as: FileWriter FW = new FileWriter ("C:\\demo.txt");


FileWriter FW = new FileWriter (String filename,boolean append);//creates a character output stream class object associated with an existing file and sets whether the stream action on the file is continued.
such as: FileWriter FW = new FileWriter ("C:\\demo.txt", ture); Indicates that when the FW writes to the file again, it will be continued at the end of the file and will not be overwritten.

2, the Main method:

void write (String str)//write String. When this method is executed, the character data is not written to the destination file. The character data is saved in the buffer.
In this case, the Refresh method can be used to save the data to the destination file.
Viod flush ()//refreshes the buffer in the stream. Save the character data in the buffer to the destination file.
Viod Close ()//closes this stream. The buffer for this stream is flushed before it is closed. The IOException exception is thrown when it is closed and then written or refreshed.

example,
3.FileWriter () Want to append content to file
try{
//new FileWriter (filename,true): True means written in append form, without true overwrites the original content
BufferedWriter writer=new BufferedWriter (New FileWriter ("E:\\xxx\\test.txt", true));
Writer.write ("Yxy");
Writer.flush ();
Writer.close ();//Must be executed

}catch (Exception e) {
E.printstacktrace ();
}

Java FileWriter class and FileReader class

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.