Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHow to Extract Content From a Text Document using Java?

How to Extract Content From a Text Document using Java?

There are several ways present in java to read the text file like BufferReader, FileReader, and Scanner.  Each and every method provides a unique way of reading the text file.

Methods:

  1. Using Files class
  2. Using FileReader class
  3. Using BufferReader class
  4. Using Scanner class

Let’s see each and every method in detail with an example to get a better understanding of the methods to, later on, implement the same to extract the content from a text document.

Method 1: Using Files class

As Java provides java.nio.file. API we can use java.nio.file.Files class to read all the contents of a file into an array. To read a text file we can use the readAllBytes() method of Files class with which using this method, when you need all the file contents in memory as well as when you are working on small files.

Example:

Java




// Java Program to Extract Content From a Text Document
// Using Files class
 
// Importing java.nio package for network linking
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
// Importing List class from java.util package
import java.util.List;
 
// Main class
public class GFG {
    // Method 1
    // To read the file using class
    private static void readUsingClass(String fileName)
        throws IOException
    {
        // Creating an object of Path class
        Path path = Paths.get(fileName);
 
        // To read file to byte array
        byte[] bytes = Files.readAllBytes(path);
 
        // Display message only
        System.out.println(
            "Read text file using Files class");
 
        // Reading the file to String List
        @SuppressWarnings("unused")
 
        // Creating a List class object of string type
        // as data in file to be read is words
        List<String> allLines = Files.readAllLines(
            path, StandardCharsets.UTF_8);
        System.out.println(new String(bytes));
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Custom input directory passed where text
        String fileName
            = "/Users/mayanksolanki/Desktop/file.txt";
 
        // read using FileReader, no encoding support, not
        // efficient
        readUsingClass(fileName);
    }
}


Output:

Method 2: Using FileReader class 

We can use java.io.FileReader can be used to read data (in characters) from files. This is a very efficient method to read the file line by line.

Syntax:

FileReader input = new FileReader(String name);

Example:

Java




// Java Program to Extract Content From a Text Document
// Using FileReader class
 
// Importing required libraries
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
// Main class
public class GFG {
    // Method
    // To read the file using File Reader
    private static void readUsingFileReader(String fileName)
        throws IOException
    {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        System.out.println(
            "Reading text file using FileReader");
        while ((line = br.readLine()) != null) {
            // process the line
            System.out.println(line);
        }
        br.close();
        fr.close();
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        String fileName
            = "/Users/mayanksolanki/Desktop/file.txt";
 
        // read using FileReader, no encoding support, not
        // efficient
        readUsingFileReader(fileName);
    }
}


Output:

Method 3: Using BufferedReader class 

If you want to read the file line by line and If you want to process on that file then you have to use BufferedReader. It is also used for processing the large file, and it supports encoding also. Reading operation is very efficient on BufferReader.

Note: Either specify the size of the BufferReader or keep the size as a Default size of BufferReader which is 8KB.  
 

Syntax:

BufferedReader in = new BufferedReader(Reader in, int size);

Implementation:

Hello I am learning web- development.
I am writing article for GFG.
I am cloud enthusiast.
I am an open-source contributor.

Note: Before starting create a text file by using .txt extension on your local machine and use the path of that file whenever you necessary while practicing. 

Example:

Java




// Java Program to Extract Content From a Text Document
// Using BufferedReader class
 
// Importing required libraries
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
// Main class
public class GFG {
    // Method
    // To read the file using Buffered Reader
    private static void
    readUsingBufferedReader(String fileName)
        throws IOException
    {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        // read file line by line
        BufferedReader br = new BufferedReader(fr);
        String line;
        System.out.println(
            "Read text file using BufferedReader");
        while ((line = br.readLine()) != null) {
            // process the line
            System.out.println(line);
        }
        // close resources
        br.close();
        fr.close();
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        String fileName
            = "/Users/mayanksolanki/Desktop/file.txt";
 
        // read using FileReader, no encoding support, not
        // efficient
        readUsingBufferedReader(fileName);
    }
}


 Output:

Method 4: Using Scanner class 

If we want to read the document based on some expression & If you want to read the document line by line then we use Scanner class. A Scanner breaks the input into tokens, which by default matches the white space. 

Example :

Java




// Java Program to Extract Content From a Text Document
// Using Scanner class
 
// Importing required libraries
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
 
// Main class
public class GFG {
   
    // Method 1
    // To read the file using Scanner
    private static void readUsingScanner(String fileName)
        throws IOException
    {
 
        // Again,  creating Path class object in main()
        // method
        Path path = Paths.get(fileName);
 
        // Creating Scanner class object to take input
        Scanner scanner = new Scanner(path);
 
        // Display message for readability
        System.out.println("Read text file using Scanner");
 
        // Now reading file line by line
        // using hasNextLine() method
        while (scanner.hasNextLine()) {
 
            // Processing each line
            String line = scanner.nextLine();
 
            // Printing processed lines
            System.out.println(line);
        }
 
        // close() method is used to close all the read
        // write connections
        scanner.close();
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Custom input text document
        // present already on the machine
        String fileName
            = "/Users/mayanksolanki/Desktop/file.txt";
 
        // Now lastly reading using FileReader
        // no encoding support, not efficient
        readUsingScanner(fileName);
    }
}


Output:

Implementation: 

Here “my_file.txt” is a demo file used for the program demonstrated below where sample lines are as follows:

Hello I am learning web- development.
I am writing article of GFG.
I am cloud enthusiast.
I am an open-source contributor.

Example 1:

Java




// Java Program to Implement Extraction of Content
// From a Text Document
 
// Importing required libraries
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
 
// Main class
public class read_file {
    // Method 1
    // To read the file using File Reader
    private static void readUsingFileReader(String fileName)
        throws IOException
    {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        System.out.println(
            "Reading text file using FileReader");
        while ((line = br.readLine()) != null) {
            // process the line
            System.out.println(line);
        }
        br.close();
        fr.close();
    }
 
    // Method 2
    // To read the file using Buffered Reader
    private static void
    readUsingBufferedReader(String fileName)
        throws IOException
    {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        // read file line by line
        BufferedReader br = new BufferedReader(fr);
        String line;
        System.out.println(
            "Read text file using BufferedReader");
        while ((line = br.readLine()) != null) {
            // process the line
            System.out.println(line);
        }
 
        // Closing resources to
        // release memory spaces
        br.close();
        fr.close();
    }
 
    // Method 3
    // To read the file using Scanner
    private static void readUsingScanner(String fileName)
        throws IOException
    {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        System.out.println("Read text file using Scanner");
        // read line by line
        while (scanner.hasNextLine()) {
            // process each line
            String line = scanner.nextLine();
            System.out.println(line);
        }
        scanner.close();
    }
 
    // Method 4
    // To read the file using class
    private static void readUsingClass(String fileName)
        throws IOException
    {
        Path path = Paths.get(fileName);
        // read file to byte array
        byte[] bytes = Files.readAllBytes(path);
        System.out.println(
            "Read text file using Files class");
        // read file to String list
        @SuppressWarnings("unused")
        List<String> allLines = Files.readAllLines(
            path, StandardCharsets.UTF_8);
        System.out.println(new String(bytes));
    }
 
    // Method 5
    // main driver method
    public static void main(String[] args)
        throws IOException
    {
        String fileName
            = "C:\\Users\\HP\\Desktop\\my_file.txt";
 
        // using Java 7 Files class to
        // process small files, get complete file data
        readUsingClass(fileName);
 
        // using Scanner class for
        // large files, to read line by line
        readUsingScanner(fileName);
 
        // read using BufferedReader, to read line by line
        readUsingBufferedReader(fileName);
 
        // read using FileReader, no encoding support, not
        // efficient
        readUsingFileReader(fileName);
    }
}


 
Example 2: Reading specific lines from a text file

Now If you want to read the specific lines from the given document then we use the BufferReader method. Depending on the file BufferReader() method is used so that our code works faster and efficiently. In this program, the Text file stored in BufferReader is a traverse through all the lines by using for loop, and when if the condition becomes true we will print that lines 

Implementation: 

myfile.txt is the demo file to be used. 

This is the sample lines been contained in this file which is consisting of random arbitral words composed in lines 

3D printing or additive manufacturing is a process of making three dimensional solid objects from a digital file.

The creation of a 3D printed object is achieved using additive processes. In an additive process an object is created by laying down successive layers of material until the object is created. Each of these layers can be seen as a thinly sliced cross-section of the object.

3D printing is the opposite of subtractive manufacturing which is cutting out / hollowing out a piece of metal or plastic with for instance a milling machine.

3D printing enables you to produce complex shapes.

It uses less material than traditional manufacturing methods.

Java




// Java program to Read Specific Lines from a Text File
 
// Importing required libraries
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initially taking empty string
        String text = "";
 
        // Declaring a variable to lookup for
        // number of lines in afile
        int lineNumber;
 
        // Try block to check for exceptions
        try {
           
            // Creating a FileReader object so as to
            // get the directory of file to be read
            FileReader readfile = new FileReader(
                "C:\\Users\\HP\\Desktop\\Exam.txt");
 
            // Creating a BufferReader class object to
            // read file as passed above
            BufferedReader readbuffer
                = new BufferedReader(readfile);
 
            // Buffer reader declaration
 
            // Conditionality for specific line/s
 
            // Loop for the traversing line by line
            // into the text file
            for (lineNumber = 1; lineNumber < 10;
                 lineNumber++) {
                // If specific line/s is found
                if (lineNumber == 7) {
                    // Store the content of this specific
                    // line
                    text = readbuffer.readLine();
                }
 
                else {
                    // Just keep on reading
                    readbuffer.readLine();
                }
            }
        }
 
        // Catching IOException exception
        catch (IOException e) {
 
            // Print the line number where exception occurred
            e.printStackTrace();
        }
 
        // Print the specific line from the file read
        System.out.println(" The specific Line is: "
                           + text);
    }
}


 
Output: The specific Line is:

3D printing enables you to produce complex shapes.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments