How to Read Text File in Java

In java applications, sometimes we need to read a text file and process it into something meaningful. There are multiple ways through which we can read text files for e.g FileReader, BufferedReader or Scanner.To read a text file line by line we can use BufferedReader or Scanner.

Below are the different ways of reading a text file in java :

1.  FileReader : It is convenient for text files in the system's default encoding.

2. FileInputStream : Files that contain weird characters.Files can be binary or text.

Reading Text Files in Java

Use FileReader if you want to read an ordinary text file in the system's default encoding, and wrapped it in a BufferedReader.

3 ways to Create a New File in Java

The first task in the java i/o is to create a new file. Creating a new file is an easy task and it can be done in 3 ways. The three ways are
1. using class
2. using class
3. using java.nio.file.Files class

1. Create a file in java using class

We will see how to create a file in java using createNewFile() method. If a file does not exist, then this method will create a file and returns true. If the file does not exist then it returns false.

import java.util.*;

public class CreateFileUsingFileClass

System.out.println in Java and How it works

System.out.println() is the most used statement in the java language.In this java tutorial we will learn what is System.out.println() and how it works. It is one of the most executed statement in the history of java. We shortly call it SOP. What is System.out.println() this question has been asked many times in java interviews.Let's find out below:

What is System.out.println

In simple words, System.out.println is a java statement that prints the argument passed, into the System.out which is generally stdout.

  • System - is a final class in java.lang package.According to Oracle docs: "Among the facilities provided by the System class are standard input, standard output, and error output streams;access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array".
  • Java Collections Framework Tutorial

    Collections in java are used in almost every enterprise application. Java Collections framework consists of interfaces and classes which helps in storing and processing the data efficiently. Java Collections Tutorial will look into each of the classes.

    Read Also :  Collections in Java Interview Questions 

    Java Collections Interfaces and Classes

  • Collection Interface
  • HashSet Class in Java Tutorial with Example

    Java HashSet class implements Set interface. It has the following properties.

    1. HashSet does not allow duplicates, means it contains unique elements.
    2. HashSet is unordered meaning it does not guarantee that the order will remain constant over time.
    3. HashSet is backed by Hashtable (actually a HashMap instance).
    4. HashSet permits the null elements.
    5. HashSet implementation is not synchronized. It can be synchronized externally.
        Set s = Collections.synchronizedSet(new HashSet(...));
    6. Iterators returned by HashSet class is fail- fast.

    HashSet Example