Java FileWriter Example

The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time.

Let us look into how FileWriter works in Java.

Constructors

The FileWriter class in Java has five parameterized constructors to allow programmers to manipulate or write to files. Based on the mode of writing, files can be overwritten or appended. Let us take a look at the constructors defined in the FileWriter class.

  • FileWriter(File file) – This constructor accepts a java.io.File object as a parameter and opens the file for writing. By default, this overwrites any previous content of the file.
  • FileWriter(FileDescriptor descriptor) This constructor creates an object of FileWriter which accepts a java.io.FileDescriptor object as a parameter and opens the file referenced by the descriptor object. By default, it overwrites the previous contents.
  • FileWriter(String filePath)This constructor accepts a java.lang.String object as a parameter and opens the file referenced by the path or URI contained in the String and overwrites any existing content in the file.
  • FileWriter(File file, boolean append) This constructor opens accepts a java.io.File and a boolean value as parameter. If the boolean value is true, the existing contents are not overwritten and the new contents are appended
  • FileWriter(String filePath, boolean append) – This constructor opens accepts a java.lang.String and a boolean value as parameter. If the boolean value is true, the existing contents are not overwritten and the new contents are appended.

In both of the last two cases, if the boolean value entered is ‘false’, the contents are overwritten.

If the file is not found or cannot be opened, a FileNotFoundException is thrown.

Methods

The FileWriter class contains methods that allow the programmer to write characters/text to a file. The FileWriter object writes the contents of the file one character at a time.

Let us take a look at the methods we use to write using FileWriter.

  • public void write(int charVal) – This method writes a single character(based on the ASCII value of the input) to the file.
  • public void write(char arr[],int offset,int l) This method writes the characters from the character array from the offset position, for a length ‘l’ of characters.
  • public void write(String str, int offset, int l) This method writes the characters from the String object from the offset position for a length ‘l’ of characters.
  • public void flush()Flushes the current FileWriter object.
  • public void close() – Closes the FileWriter object to prevent any resource leaks.

Example

Let us look at an example of writing to a file with FileWriter class.

Code:

Output:

New Write Complete. File Reads:Hello. I’m writing this new.
Append Complete. File Reads
Hello. I’m writing this new. Append works too

Thats all about java FileWriter example.

Related Posts

  • Learn java students
    01 September

    Is It Promising for Students to Learn Java?

    Table of ContentsWhat Is Java?How It Was Developed?Reasons Students Should Learn JavaWhere Is It Used?Who Can You Work With?Where Exactly Can Java Be Learned? Coding has become a very important part of the job industry, taking over almost every profession. You are required to have a basic knowledge of languages like HTML and CSS, if […]

  • Get current year in java
    13 July

    Get current year in Java

    Table of ContentsUsing Date and SimpleDateFormatUsing CalendarUsing LocalDateUsing LocalDateTimeusing ZonedDateTimeUsing OffsetDateTimeUsing Instant and ZoneIdUsing Instant and ZoneOffsetConclusion In this post, we will see how to get current year in Java. Using Date and SimpleDateFormat The Date class was introduced in Java 1.0 and provided different approaches to work with time. The SimpleDateFormat inherits from DateFormat […]

  • 27 March

    Top Object-Oriented Programming Languages to Follow in 2021

    Table of ContentsJavaProsConsWhere ApplicableC/C++ProsConsWhere ApplicablePythonProsConsWhere ApplicableJavaScriptProsConsWhere ApplicableR LanguageProsConsWhere ApplicableConclusion Wherever you look, you face object-oriented programming languages. Well, many of the popular coding languages are object-oriented. The moment you decide to learn an object-oriented programming language, it might be quite overwhelming to pick the only one from this big pool of options. But don’t worry! […]

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 02 January

    System.out.println shortcut in eclipse

    Learn about System.out.println shortcut in eclipse.

  • 19 September

    Java 9 – Anonymous Inner classes and Diamond Operator

    Table of ContentsAnonymous class: Java 9 ImprovementExample: Anonymous class with diamond operator Type inference is a feature of Java that was introduced in Java 7. Now, Java compiler can infer type of a data automatically. For example, if we create an ArrayList then we use the code like: [crayon-6170215387bb6256487698/] You can see that Java 7 […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.