Class FileWriter in Java

digitization, transformation, binary

Class FileWriter in Java

FileWriter is a convenience subclass of OutputStreamWriter that is useful when we want to write text (as opposed to binary data) to a file. We create a FileWriter by specifying the file to be written to, and optionally specifying whether the data should be appended to the end of an existing file instead of overwriting that file.

The FileWriter class represents a character stream that writes data to a file. It is a subclass of OutputStreamWriter that uses a default buffer size (8192 bytes) to write bytes to a file and the default character encoding scheme to convert characters to bytes. If you need to specify the character encoding or the buffer size, wrap an OutputStreamWriter around a FileOutputStream.

Ads code goes here

The file can be specified using a FileDescriptor, a File object, or a String that represents a pathname. All of the constructors can throw a SecurityException if the application does not have permission to write to the specified file.

FileWriter provides a low-level interface for writing character data to a file. We can think about wrapping a FileWriter with a BufferedWriter to increase writing efficiency.

If we need to write binary data to a file, we can use a FileOutputStream wrapped by a DataOutputStream or a PrintStream instead.

The FileWriter class creates an internal FileOutputStream to write bytes to the specified file, and uses the functionality of its superclass, OutputStreamWriter, to convert the Unicode characters written to the stream characters into bytes using the default encoding of the default locale. (If we want to use an encoding other than the default, we cannot use FileWriter; in that case, we must create your own OutputStreamWriter and FileOutputStream.)

READ  Java Documentation-JavaDoc Concept Simplified in Java

Because FileWriter is a trivial subclass of OutputStreamWriter, it does not define any methods of its own, but simply inherits them from its superclass.

The structure of the class FileWriter is given by

public class java.io.FileWriter extends java.io.OutputStreamWriter{
// Public Constructors
public FileWriter(String fileName) throws IOException;
public FileWriter(String fileName, boolean append) throws IOException;
public FileWriter(File file) throws IOException;
public FileWriter(FileDescriptor fd);
}

The details of the class structure are given as follows:

public FileWriter(String fileName) throws IOException;

public FileWriter(String fileName) constructor creates a FileWriter that sends its output to the file named by the specified String.
Parameter
fileName -The pathname of the file to use as output.

public FileWriter(String fileName, boolean append) throws IOException;

public FileWriter(String fileName, boolean append) constructor creates a FileWriter that sends its output to the named file. If append is true, the stream is positioned at the end of the file, and data is appended to the end of the file. Otherwise, if the append is false, the stream is positioned at the beginning of the file, and any previous data is overwritten.
Parameter
fileName– The pathname of the file to use as output.
append – Specifies whether or not data is appended to the output stream.

public FileWriter(File file) throws IOException;

public FileWriter(File file) constructor creates a FileWriter that sends its output to the file represented by the
specified File object.
Parameter
file – The File to use as output.

public FileWriter(FileDescriptor fd);

public FileWriter(FileDescriptor fd) constructor creates a FileWriter that sends its output to the file identified by the given FileDescriptor.
Parameter
fd -The FileDescriptor of the file to use as output.
Apart from these FileWriter class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)
READ  Abstract Interface EventListener in Java

FileWriter class also has inherited methods from OutputStreamWriter. They are as follows:

  • flush()
  • getEncoding()
  • write(int)
  • write(char[],int, int)
  • write(String,int, int)
  • close()

FileWriter class also has inherited methods from Writer. They are as follows:

  • write(char[])
  • rite(String)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *