Use FileWriter for java file operations, insert content to the end of the file, javafilewriter

Source: Internet
Author: User

Use FileWriter for java file operations, insert content to the end of the file, javafilewriter

Original article: FileWriter usage for java file operations, insert content to the end of the file

Code: http://www.zuidaima.com/share/1550463258610688.htm

Insert content to the end of the file through FileWriter, as long as you set append = true during the construction. FileWriter fileWritter = new FileWriter (file. getName (), true); after the code is run, the file is generated under the root directory of the project.


In Java, the specific usage of PrintWriter and FileWriter and the methods they use together

If it is true, the data is written to the end of the file, instead of the start of the file.

The second line is to create a PrintWriter object to use this object to write data to the names.txt file.

An error occurred while calling FileWriter to read the file in java.

Public int read (char [] cbuf)
Read characters into the array.
Return Value:
Number of characters read. If the number of characters reaches the end of the stream,-1 is returned.

Because the last read operation includes the last few or dozens of Characters in the character array and the last value, some other characters are added at the end of the written file.

Solution:
Int readlen = 0;
While (readlen = fr. read (count ))! =-1)
{
Fw. write (count, 0, readlen );
}

MATERIALS:
Public abstract void write (char [] cbuf,
Int off,
Int len)
Write a part of the character array.
Parameters:
Cbuf-character array
Off-offset at the start of writing characters
Len-number of characters to be written

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.