×

Java file Writer

File Writer:

It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant of writing a set of characters. It creates an output file.

Writer Class:

It is a class of Java.io package, it represents the set of characters. It is an abstract class.

Writer Subclasses:

  • Buffered Writer
  • File Writer
  • String Writer
  • Output Stream Writer

Writer class object creation Syntax:

Writer object1 = new FileWriter();

Note: Here, to create an object we use FileWriter because the Writer class is abstract.

Constructors in File Writer:

  • FileWriter (File f)
  • FileWriter (File f, boolean ap)
  • FileWriter (FileDescriptor fdes)
  • FileWriter (String fN)
  • FileWriter (String fN, Boolean ap)
  • FileWriter (File f, Charset chset)
  • FileWriter (File f, Charset chset, boolean ap)
  • FileWriter (String fN, Charset chset, boolean ap)

FileWriter (File f):It creates or generates a file Writer object. It throws n IOException i.e, Input or Output Exception.

Syntax for creating object:

FileWriter f = new FileWriter(File f);

FileWriter (File f, boolean ap): It creates or generates a file writer object. If the given second argument is true then, it will be written the end of the file rather than at the beginning. It throws an IOException i.e, Input or Output Exception.

Syntax for creating object:

FileWriter f = new FileWriter(File f, boolean ap);

FileWriter (FileDescriptor fdes):It creates or generates a file writer object which is associated with the file descriptor.

Syntax for creating object:

FileWriter f = new FileWriter(FileDescriptor fdes);

FileWriter (String fN):It creates or generates a file writer object, given the file name.

Syntax for creating object:

FileWriter f = new FileWriter(String fN);

FileWriter (String fN, Boolean ap): It creates or generates a file writer object given a file name, it indicates whether to append or not to append the data written.

Syntax for creating object:

FileWriter f = new FileWriter(String fN, Boolean ap);

FileWriter (File f, Charset chset):  It creates or generates the file object when the file and charset are available.

Syntax for creating object:

 FileWriter f = new FileWriter(File f, Charset chset);

FileWriter (File f, Charset chset, boolean ap): It creates or generates the file object when the file and charset are given and if the argument is true then it appends to the end of the file rather than the beginning.

Syntax for creating object:

FileWriter f = new FileWriter(File f, Charset chest, boolean ap); 

FileWriter (String fN, Charset chest, boolean ap): It creates a file object when file and charset are given.

Syntax for creating object:

FileWriter f = new FileWriter(String fN, Charset chest, boolean ap);

Methods in File Writer:

  • write (int n)
  • write (String s1, int pos1, int si)
  • write (char c[], int po1, int si)

write (int n):
It is used to write a single character of data. The return type is a void, it overrides the write method which is present in the Writer class. It accepts only one parameter i.e, Integer type. It throws IOException which is an Input / Output Exception when an error occurs in Input or Output.

Syntax:

public void write(int n)

Program:

import java.io.FileWriter;
  
class Example1 {
  
    public static void main(String args[])
    {
  
        String data = "Welcome to Javatpoint";
          try {
            FileWriter output = new FileWriter("output.txt");
            output.write(data);
            output.close();
        }
  
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output:

Welcome to Javatpoint

Here, the string is written into the file output.txt then the output.txt file contains Welcome to Javatpint.

write (String s1, int pos1, int si):

It is used to write the part of the string when it is passed through the Writer stream. It throws Input / Output Exception and Index out of bounds exception. I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than string.

Syntax:

public int read(String s1, int pos1, int si)
  • s1 -It is the Destination buffer
  • pos1 – at which index it starts writing characters.
  • si -.Several characters

Program:

import java.io.*;
  class C{
    public static void main(String[] args) 
    {
   String s1 = “Welcome to JavaTpoint”
     try {
            FileWriter output = new FileWriter("output.txt");
            output.write(s1,0,4);
            output.close();
        }
  
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output:

Welcome

write (char c[], int po1, int si):

It writes the characters from a particular array. , it directly uses the main Stream. It throws Input / Output Exception and Index out of bounds exception. I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than an array.

Syntax:

public int read(char[] c, int po1, int si)
  • c -It is the Destination buffer
  • po1 – at which index it starts writing characters.
  • si -.Several characters

Program:

import java.io.*;
  class C{
    public static void main(String[] args) 
    {
   char ch[] = [‘W’,’E’,’L’,’C’,’O’,’M’,’E’];
     try {
            FileWriter output = new FileWriter("output.txt");
            output.write(ch,0,2);
            output.close();
        }
  
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output:

WE

Related Topics

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the...

4 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.