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