YouTip LogoYouTip

Java Filewriter

# Java FileWriter Class [![Image 3: Java Stream](#) Java Stream](#) * * * The FileWriter class is inherited from the OutputStreamWriter class. This class writes data to a stream character by character. You can create the required object using one of the following constructors. Constructs a FileWriter object given a File object. FileWriter(File file) Constructs a FileWriter object given a File object. FileWriter(File file, boolean append) **Parameters:** * **file**: The File object to write data to. * **append**: If the append parameter is true, bytes are written to the end of the file, equivalent to appending information. If the append parameter is false, bytes are written to the beginning of the file. Constructs a FileWriter object associated with a file descriptor. FileWriter(FileDescriptor fd) Constructs a FileWriter object given a file name, with a boolean value indicating whether to append data. FileWriter(String fileName, boolean append) After successfully creating a FileWriter object, you can manipulate the file using the methods listed below. | No. | Method Description | | --- | --- | | 1 | **public void write(int c) throws IOException** Writes a single character c. | | 2 | **public void write(char [] c, int offset, int len)** Writes a portion of a character array, starting at offset and of length len. | | 3 | **public void write(String s, int offset, int len)** Writes a portion of a string, starting at offset and of length len. | ### Example ## Example import java.io.*; public class FileRead{public static void main(String args[])throws IOException{File file = new File("Hello1.txt"); // Create file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); // Write content to the file writer.write("Thisn isn ann examplen"); writer.flush(); writer.close(); // Create FileReader object FileReader fr = new FileReader(file); char[]a = new char; fr.read(a); // Read content from the array for(char c : a)System.out.print(c); // Print characters one by one fr.close(); }} The result of compiling and running the above example is as follows: Thisis an example * * Java Stream](#)
← Java InheritanceJava Filereader β†’