Description of common methods of FileWriter class in Java

Time:2022-8-5

FileWriter character output stream

Construction method


public FileWriter(String fileName) throws IOException

Constructs a FileWriter object from the given filename.

fileName – A string representing the system-dependent filename.


public FileWriter(String fileName,boolean append) throws IOException

Constructs a FileWriter object from the given filename and a boolean value indicating whether to append write data.

fileName – A string representing the system-dependent filename.

append – A boolean value that, if true, writes data at the end of the file rather than at the beginning.


public FileWriter(File file) throws IOException

Constructs a FileWriter object from the given File object.

file – The File object to write the data to.

Common method


public void write(int c):

write a single character


public void write(char[] cbuf):

write character array


public abstract void write(char[] cbuf, int off,  int len):

write part of a character array


public void write(String str):

write string


public void write(String str,int off, int len):

write part of a string


public void flush() throws IOException

Flushes the stream's buffer.


public void close() throws IOException

Close this stream, but flush it first

Usage of FileWriter and FileReader

java: IO streams (readLine() and newLine() methods)

package com.itcast.demo4;
import java.io.*;
public class Java_2 {
    public static void main(String args[]) {
        String ShowMes[] = {"There is a group of Smurfs on the other side of the mountain and the sea", "They are lively and smart, they are naughty and sensitive", "They live freely in the big green forest", "They are kind Be brave and rejoice with each other!"};
        try {
            //*********Found********
            FileWriter out = new FileWriter("test.txt");
            BufferedWriter outBW = new BufferedWriter(out);
            for (int i = 0; i < ShowMes.length; i++) {
                outBW.write(ShowMes[i]);
                outBW.newLine();//Cross-platform line break
                //outBW.write(&quot;\r\n&quot;);//Only support windows system
            }
            //*********Found********
            outBW.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //*********Found********
            FileReader in = new FileReader(new File("test.txt"));
            BufferedReader inBR = new BufferedReader(in);
            String stext = null;
            int j = 1;
            while ((stext = inBR.readLine()) != null) {
                System.out.println(&quot;Line &quot; + j + &quot;: &quot; + stext);
                //*********Found********
                j++;
            }
            inBR.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

The above is a personal experience, I hope it can give you a reference, and I hope you can support developpaer a lot.

Recommended Today

Go implements various current limiting

foreword When developing a high-concurrency system, we may encounter that the frequency of interface access is too high. In order to ensure the high availability and stability of the system, we need to limit the flow at this time. You may useNginxthisWeb ServerTo control the request, it may also be implemented using some popular class […]