[Java usage] detailed explanation of the usage of FileWriter, wirte() method and several overloaded methods

Contents of this article

1, Usage introduction

2, Question answer

2.1. What happens if this file does not exist?

2.2. What happens if this file exists?

2.3. If you don't want to overwrite the previous content, what should you do if you want to add content?

2.4. How to realize line feed when adding data?

3, Usage expansion

3.1. Overload of write method

3.2 example of heavy load

1, Usage introduction

The usage of FileWriter is very simple, which can be summarized into three words: create (new), write (write), close (close);

Create (New): This is the new we usually use;

Write: write out the content using one of the overloaded methods write();

Close: close the flow;

The following is a specific example:

public class TestFileWriter {

    public static void main(String[] args) throws IOException {
        // Method 1: the created file will be in the project
        FileWriter fileWriter1 = new FileWriter("file01.txt");
        fileWriter1.write("I am very proud that I am Chinese!");
        fileWriter1.write("No regrets in this life!");
        fileWriter1.write("\r\n Since ancient times, no one has died. Keep your heart in history!");
        fileWriter1.close();
        
        // Method 2: the created file will be in the specified directory (note that you must have write permission here, otherwise the creation will fail!)
        FileWriter fileWriter2 = new FileWriter("G:\\aaa\\file02.txt");
        fileWriter2.write("I am very proud that I am Chinese!");
        fileWriter2.close();
    }
}

Output results:

2, Question answer

Here are some common questions summarized:

2.1. What happens if this file does not exist?

If this file does not exist, it will help us automatically create one. Where will the plain text file be placed after creation? If you do not specify a directory, it will be placed under the Java project, at the same level as the src directory and target directory.
    
If the directory is specified, the file will be placed in the specified directory: as in mode 2 above; FileWriter fileWriter2 = new FileWriter("G:\aaa\file02.txt");

2.2. What happens if this file exists?

If the plain text file already exists and there is content in it, we will overwrite the existing content of the plain text file by adding more content.

2.3. If you don't want to overwrite the previous content, what should you do if you want to add content?

In fact, this is also very simple. You only need to select another construction method (overloaded construction method) when creating an object.

FileWriter fw = new FileWriter("file01.txt", true);

Or:

FileWriter fileWriter2 = new FileWriter("G:\\aaa\\file02.txt", true);

2.4. How to realize line feed when adding data?

The data added to a plain text file is on the same line, so how to wrap it?

Pay attention to distinguishing operating systems:

Under Windows operating system: \ r\n
    
Under Linux operating system: \ n
    
Under Mac operating system: in the early days, it was used \ r, but now it is used \ n

For example: filewriter1 Write ("who has never died in life since ancient times, \ r\n keep your heart in history!");

3, Usage expansion

3.1. Overload of write method

write() can be overloaded in five ways:

write(int c): write a number and convert it using ASCII code table or Unicode table
    
write(String str): writes a string
    
write(char[] cbuf): write character array
    
write(String str, int off, int len): writes a part of a string
    
write(char[] cbuf,int off,int len): writes a part of the character array

Commonly used in ASCII code table: 48 - 0, 65 - A, 97 - a

3.2 example of heavy load

Specific examples:

 public static void testOverload() throws IOException {
        FileWriter fileWriter = new FileWriter("file03.txt");
        // Overload method 1: write numbers and convert them using ASCII code table or Unicode table
        // Commonly used in ASCII code table: 48 - 0, 65 - A, 97 - a
        fileWriter.write(65);
        fileWriter.write("\r\n");

        // Define string
        String str = "I am very proud that I am Chinese!";
        // Overload method 2: write string
        fileWriter.write(str);
        fileWriter.write("\r\n");
        // Overload method 3: write character array
        char[] javaArray = {'J','a','v','a'};
        fileWriter.write(javaArray);
        fileWriter.write("\r\n");
        // Overload method 4: write part of the string (result: I'm Chinese)
        fileWriter.write(str, 4, 5);
        fileWriter.write("\r\n");
        // Overload method 5: write a part of the character array (result: v)
        fileWriter.write(javaArray, 2, 1);

        fileWriter.close();
    }

The output result is:

 

end!

Keywords: Java Linux Windows

Added by plimpton on Sun, 19 Dec 2021 19:38:10 +0200