Writing into files

  • Eclipse: Oxygen
  • Java: 1.8

Writing into files

The file writing includes the use of Buffered Writer and File Writer in order to write any statement into the file.

In the following program first, we create a file object. To write content into the file we must use “BufferedWriter”.  This is used to write single arrays, characters, and strings. FileWriter is also a convenient class for writing text files using the default character encoding of the operating system.

But BufferedWriter writes text to a character stream with efficiency and provides a convenient method for writing a line separator: newLine().

A writer uses the default character encoding of the operating system by default. It also creates a new file if it does not exist or overwrite the existing one. If you want to add text to an existing file, pass a true Boolean flag to a constructor of the writer class that tells the FileWriter for the append mode. True means that the append mode is on.

Reading files with BufferredReader

Here are several built-in methods of the String data type. We will learn about the further use of them in these documents.

This deals with reading the statements from the file. It also follows the same procedure and the import of the same libraries. To read the files we use File Reader and Buffered Reader.

BufferedReader is a class in Java that is used to read the text of a character-based input stream. It can be used to read data line by line using the readLine() method. It makes the performance fast.

In the following program, we are reading the data from the text file “team.txt” using the Java BufferedReader.

Reading files with a scanner

In Java as seen above not only we can read files with the help of File Reader but also scanner class by using some functions like the hasnext. Therefore we can read the file statements by the same class.

Here is a simple code that demonstrates how we can read a text file line by line using the scanner. Use nextLine () to read a file line by line.

In the following program, we use the hasNext () method to check if there are more lines left and then use nextLine () to read the line. Do not forget to close the Scanner once you have finished avoiding the leakage of resources.

Scanner vs Buffered reader

The buffered reader and the scanner are compared and their pros and cons are analyzed here. Therefore, the conclusion is that the buffer class should be preferred because of its speed size and synchronous behavior.

                     BufferedReader class                                        Scanner class
  • BufferedReader is synchronous
  • BuffredReader should be used within multiple threaded application
  • Larger Buffer (8KB byte buffer)
  • BufferReader is a bit faster as BufferedReader simply reads the sequence of characters.
  • Scanner is not synchronous
  • Scanner should be avoided in multiple threaded application
  • Small buffer(1KB char buffer)
  • Scanner is a little slower, as scanner does parsing of input data

 

Contributed by Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu