FileWriter class methods in Java

java.io.FileWriter

Writes text to character files using a default buffer size. Encoding from characters to bytes uses either a specified charset or the platform's default charset.

The FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

Some methods of FileWriter class

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, boolean)This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(File, Charset)This method constructs a FileWriter given the File to write and charset.

FileWriter(String, boolean)This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, Charset)This method constructs a FileWriter given a file name and charset.

FileWriter(String, Charset, boolean)This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.

FileWriter(File, Charset, boolean)This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.


FileWriter FileWriter(File, Charset, boolean) in Java

FileWriter(File, Charset, boolean): This method is available in the java.io.FileWriter class of Java.

Syntax:

java.io.FileWriter.FileWriter(File file, Charset charset, boolean append) throws IOException

This method takes three arguments. This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.

Parameters: Three parameters are required for this method.

file: the File to write.

charset: the charset.

append: a boolean. If true, the writer will write the data to the end of the file rather than the beginning.

Throws:

IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter9 {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        Charset charset = Charset.defaultCharset();
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, charset, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

java.io.FileWriter@26f0a63f


Approach 2: IOException 

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter9 {
    public static void main(String[] args) {

        String pathname = "D:\\hello2.txt";
        File file = new File(pathname);
        Charset charset = Charset.defaultCharset();
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, charset, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

Error occured

Some other methods of FileWriter.

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, boolean)This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(File, Charset)This method constructs a FileWriter given the File to write and charset.

FileWriter(String, boolean)This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, Charset)This method constructs a FileWriter given a file name and charset.

FileWriter(String, Charset, boolean)This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.


FileWriter FileWriter(String, Charset, boolean) in Java

FileWriter(String, Charset, boolean): This method is available in the java.io.FileWriter class of Java.

Syntax:

java.io.FileWriter.FileWriter(String fileName, Charset charset, boolean append) throws IOException

This method takes three arguments. This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.

Parameters: Three parameters are required for this method.

fileName: the name of the file to write.

charset: the charset.

append a boolean. If true, the writer will write the data to the end of the file rather than the beginning.

Throws:

IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

Approach 1: When no exception

Java

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter8 {
    public static void main(String[] args) {

        String filename = "D:\\hello.txt";
        Charset charset = Charset.defaultCharset();
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter =
new FileWriter(filename, charset, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

java.io.FileWriter@26f0a63f


Approach 2: IOException 

Java

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter8 {
    public static void main(String[] args) {

        String filename = "D:\\hello2.txt";
        Charset charset = Charset.defaultCharset();
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter =
new FileWriter(filename, charset, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

Error occured


Some other methods of FileWriter.

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, boolean)This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(File, Charset)This method constructs a FileWriter given the File to write and charset.

FileWriter(String, boolean)This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, Charset)This method constructs a FileWriter given a file name and charset.

FileWriter(File, Charset, boolean)This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.



FileWriter FileWriter(String, Charset) in Java

FileWriter(String, Charset): This method is available in the java.io.FileWriter class of Java.

Syntax:

java.io.FileWriter.FileWriter(String fileName, Charset charset) throws IOException

This method takes two arguments. This method constructs a FileWriter given a file name and charset.

Parameters: Two parameters are required for this method.

fileName: the name of the file to write.

charset: the charset.

Throws:

IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

Approach 1: When no exception

Java

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter7 {
    public static void main(String[] args) {

        String filename = "D:\\hello.txt";
        Charset charset = Charset.defaultCharset();
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filename, charset);
return;
        } catch (IOException e) {
            System.out.println("Error occured");
        }

        System.out.println(fileWriter);
    }
}

Output:

java.io.FileWriter@26f0a63f


Approach 2: IOException

Java

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter7 {
    public static void main(String[] args) {

        String filename = "D:\\hello2.txt";
        Charset charset = Charset.defaultCharset();
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filename, charset);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

Error occured


Some other methods of FileWriter.

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, boolean)This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(File, Charset)This method constructs a FileWriter given the File to write and charset.

FileWriter(String, boolean)This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, Charset, boolean)This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.

FileWriter(File, Charset, boolean)This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.



FileWriter FileWriter(String, boolean) in Java

FileWriter(String, boolean): This method is available in the java.io.FileWriter class of Java.

Syntax:

java.io.FileWriter.FileWriter(String fileName, boolean append) throws IOException

This method takes two arguments. This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

Parameters: Two parameters are required for this method.

fileName: String The system-dependent filename.

append: boolean if true, then data will be written to the end of the file rather than the beginning.

Throws:

IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason

Approach 1: When no exception

Java

import java.io.FileWriter;
import java.io.IOException;

public class FileWriter6 {
    public static void main(String[] args) {

        String filename = "D:\\hello.txt";
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filename, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

java.io.FileWriter@26f0a63f


Approach 2: IOException

Java

import java.io.FileWriter;
import java.io.IOException;

public class FileWriter6 {
    public static void main(String[] args) {

        String filename = "D:\\hello2.txt";
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filename, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

Error occured



Some other methods of FileWriter.

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, boolean)This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(File, Charset)This method constructs a FileWriter given the File to write and charset.

FileWriter(String, Charset)This method constructs a FileWriter given a file name and charset.

FileWriter(String, Charset, boolean)This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.

FileWriter(File, Charset, boolean)This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.



FileWriter FileWriter(File, Charset) in Java

FileWriter(File, Charset): This method is available in the java.io.FileWriter class of Java.

Syntax:

java.io.FileWriter.FileWriter(File file, Charset charset) throws IOException

This method takes two arguments. This method constructs a FileWriter given the File to write and charset.

Parameters: Two parameters are required for this method.

file: the File to write.

charset: the charset.

Throws:

IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

Approach 1: When no exceptions

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter5 {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        Charset charset = Charset.defaultCharset();
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, charset);
return;
        } catch (IOException e) {
            System.out.println("Error occured");
        }

        System.out.println(fileWriter);
    }
}

Output:

java.io.FileWriter@26f0a63f


Approach 2: IOException 

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileWriter5 {
    public static void main(String[] args) {

        String pathname = "D:\\hello2.txt";
        File file = new File(pathname);
        Charset charset = Charset.defaultCharset();
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, charset);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

Error occured



Some other methods of FileWriter.

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, boolean)This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, boolean)This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, Charset)This method constructs a FileWriter given a file name and charset.

FileWriter(String, Charset, boolean)This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.

FileWriter(File, Charset, boolean)This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.



FileWriter FileWriter(File, boolean) in Java

FileWriter(File, boolean): This method is available in the java.io.FileWriter class of Java.

Syntax:

java.io.FileWriter.FileWriter(File file, boolean append) throws IOException

This method takes two arguments. This method constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

Parameters: One parameter is required for this method.

file: the File to write.

append: if true, then bytes will be written to the end of the file rather than the beginning.

Throws:

IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriter4 {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

java.io.FileWriter@26f0a63f


Approach 2: IOException

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriter4 {
    public static void main(String[] args) {

        String pathname = "D:\\hello2.txt";
        File file = new File(pathname);
        boolean append = true;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file, append);
        } catch (IOException e) {
            System.out.println("Error occured");
            return;
        }

        System.out.println(fileWriter);
    }
}

Output:

Error occured



Some other methods of FileWriter.

FileWriter(File)This method constructs a FileWriter given the File to write, using the platform's default charset.

FileWriter(FileDescriptor)This method constructs a FileWriter given a file descriptor, using the platform's default charset.

FileWriter(String)This method constructs a FileWriter given a file name, using the platform's default charset.

FileWriter(File, Charset)This method constructs a FileWriter given the File to write and charset.

FileWriter(String, boolean)This method constructs a FileWriter given a file name and a boolean indicating whether to append the data written, using the platform's default charset.

FileWriter(String, Charset)This method constructs a FileWriter given a file name and charset.

FileWriter(String, Charset, boolean)This method constructs a FileWriter given a file name, charset, and a boolean indicating whether to append the data written.

FileWriter(File, Charset, boolean)This method constructs a FileWriter given the File to write, charset, and a boolean indicating whether to append the data written.