Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: IO. No Comments on Writing Data to a File in Java
0
(0)

In this article we will look at different ways for writing data to a file in Java programming language.

 

There are different ways for reading data from a text file which can be used for different types of applications. The multiple ways for reading data from a text file include using BufferedWriter, FileWriter, FileOutputStream, etc.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Each way for reading provides different features like while reading text using BufferedWriter, it provides buffering capability which allows the program to read text faster. While using FileOutputStream, we can store raw data (binary data) in a text file.

 

Using BufferedWriter Class

This way of writing text to a file writes it as a character output stream. It can buffer the text for efficient writing of lines, arrays, and characters. The buffer size can be left as default or can be changed base on our need.

 

In general, the write requests made by FileWriter make a request to the underlying character and byte stream. So, it is better if those requests can be wrapped using BufferedWriter as shown below:

 

BufferedWriter out = new BufferedWriter(Writer out, int size);

 

Let’s see a program which uses BufferedWriter class for writing data to a file. In the below example, the text file name is “output.txt”.

import java.io.*;
class Driver
{   
    public static void main(String[] args) throws Exception
    {
		File f = new File("output.txt");
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		String text1 = "Writing data to a text file.";
		String text2 = "This is a sample string to test writing to a 		file.";
		bw.write(text1);
		bw.write(System.lineSeparator());
		bw.write(text2);
		System.out.println("Data stored in the file successfully.");
		bw.close();
    }
}

 

The output of the above program is:

Data stored in the file successfully.

 

Using FileWriter Class

Writing to a file using FileWriter class provides convenience for writing the text character by character. The constructor of FileWriter class provides default encoding and buffer size in bytes.

 

Let’s see a program which uses FileWriter class for writing data to a file. In the below example, the text file name is “output.txt”.

import java.io.*;
class Driver
{   
    public static void main(String[] args) throws Exception
    {
		File f = new File("output.txt");
		FileWriter fw = new FileWriter(f);
		String text1 = "Writing data using FileWriter to a text 			file.";
		String text2 = "This is a sample string to test writing to a 		file.";
		fw.write(text1);
		fw.write(System.lineSeparator());
		fw.write(text2);
		System.out.println("Data stored in the file successfully.");
		fw.close();
    }
}

 

The output of the above program is:

Data stored in the file successfully.

 

Using FileOutputStream Class

By using FileOutputStream class we can store/write binary or raw data into a file. This cannot be achieved using FileWriter and BufferedWriter classes. While writing content to a file using FileOutputStream class, we need to first convert it into bytes to store it as raw data.

 

Let’s see a program which uses FileOutputStream class for writing data to a file. In the below example, the text file name is “output.txt”.

import java.io.*;
class Driver
{   
    public static void main(String[] args) throws Exception
    {
		File f = new File("output.txt");
		FileOutputStream  fos = new FileOutputStream(f);
		String text1 = "Writing data using FileOutputStream to a text 		file.";
		String text2 = "This is a sample string to test writing to a 		file.";
		byte[] tb1 = text1.getBytes();
		byte[] tb2 = text2.getBytes();
		fos.write(tb1);
		fos.write(System.lineSeparator().getBytes());
		fos.write(tb2);
		System.out.println("Data stored in the file successfully.");
		fos.close();
    }
}

 

The output of the above program is:

Data stored in the file successfully.

 

For more information visit the following links:

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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