Java FileWriter Example

Java FileWriter

  • Java FileWriter class is a part of java.io package.
  • FileWriter is a sub class of java.io.OutputStreamWriter class.
  • FileWriter is meant for writing streams of characters.
  • FileWriter is used to write to character files. Its write() methods allow you to write character(s) or strings to a file.
  • FileWriters are usually wrapped by higher-level Writer objects, such as BufferedWriter or PrintWriter, which provide better performance and higher-level, more flexible methods to write data.
  • Java-FileWriter

FileWriter Constructors

  1. FileWriter(File file): Creates a FileWriter object using specified File object. It throws an IOException if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.
  2. FileWriter(File file, boolean append): Creates a FileWriter object using specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. It throws an IOException if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.
  3. FileWriter(FileDescriptor fd): Creates a FileWriter object associated with specified file descriptor.
  4. FileWriter(String fileName): Creates a FileWriter object using specified fileName. It throws an IOException if the named file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.
  5. FileWriter(String fileName, boolean append): Creates a FileWriter object using specified fileName with a boolean indicating whether or not to append the data written. If the second argument is true, then the data will be written to the end of the file rather than the beginning. It throws an IOException if the named file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.

Java FileWriter Example

FileWriter inherits the method of java.io.OutputStreamWriter and java.io.Writer classes.

Let’s have a look at the below methods with examples.

write(int c)

This method writes a single character specified by int c.

FileWriter implements AutoCloseable interface, hence we can use try with resources while using FileWriter class.

Note: In the above program fileWriter.write(65) will write A into file because the 65 is the decimal value for the character A, hence integer 65 will be converted into character A and same for the other.

write(String str, int off, int len)

This method writes a portion of String str from int off to int len.

  • str: String to be written
  • off: Offset from which to start reading characters
  • len: Number of characters to be written

If the value of the len parameter is negative then no characters are written.

write(char[] cbuf, int off, int len)

This method writes a portion of an array of characters specified by char[] cbuf from int off to int len.

  • cbuf: A character array
  • off: Offset from which to start reading characters
  • len : Number of characters to write

write(char[] cbuf)

This method writes the array of character specified by cbuf.

write(String str)

This method writes a string value into file specified by str.

append(char c)

This method appends the specified character to this writer where c is the 16-bit character to append.

flush()

This method flushes the stream. When flush() method is called it immediately writes the data to the output file.

close()

This method flush the stream before close it. Once the stream has been closed, invocation of write() or flush() method will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Output:

FileWriter vs FileOutputStream

  • FileWriter is meant for writing streams of characters while FileOutputStream is used for writing streams of raw bytes.
  • FileWriter deal with 16-bit characters while FileOutputStream deals with 8-bit bytes.
  • FileWriter can handle Unicode strings while FileOutputStream writes bytes to a file and do not accepts characters or strings hence it needs to wrapped up by OutputStreamWriter to accept strings.

Also check java write file for more about how to write file in java.

That’s all for Java FileWriter, I hope nothing important got missed here.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: