What is the fastest way to read a file in Java?

What is the fastest way to read a file in Java?

Hi,

I am reading my file using the Scanner class. My code is below:

import java.io.*;
import java.util.Scanner;

class  filereader
{
    public static void main(String[] args) 
    {

        try{
          Scanner sc = new Scanner(new FileReader("myfile.txt"));

        while (sc.hasNextLine()) {
        String text = sc.nextLine();
                //Business code here
        }

        }catch(Exception e){
            System.out.println("Error is: "+ e.getMessage());
        }

    }
}

I have to read text file of 10GB and then process the lines for getting the data. Let's know the fasted way of reading the text.

Thanks

View Answers

February 14, 2015 at 5:40 PM

Since you have to read the text file of big size you should use the BufferedReader class of Java.

Here is the sample code you can use:

FileInputStream fstream = new FileInputStream("textfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)   {
    System.out.println (strLine);
}

Read the details at How to read file line by line in Java?


February 14, 2015 at 5:50 PM

The BufferedReader and the BufferedReader classes are developed to read from the input stream.

The Scanner class provides more features such as parsing the tokens from the input stream while BufferedReader just reads the data from the input streams.

So, if you don't have any requirement of parsing the data while reading you simply use the BufferedReader class.

The BufferedReader class is used to read (just) read the data from the input streams much faster. In the case of reading the data one line at a time from the input stream just use the BufferedReader class.

Thanks









Related Tutorials/Questions & Answers:
What is the fastest way to read a file in Java?
What is the fastest way to learn AI?
Advertisements
difference between java5 and java6 - Java Beginners
Javah -  Header File Generator
What is the best way to write to file in Java?
What is the fastest growing careers?
Java2
Javah
Javah - Header File Generator
what fastest DataStructre(efficient) in java? and why? - Java Beginners
Javah - Header File Generator
read a file
about java1
file read
Fastest way to upload huge raw data in database - c#, Java or stored procedure
About Java2
What is dirty read?
What is the best way to learn Bootstrap?
What is the best way to learn Bootstrap?
What is the best way to learn JavaScript?
What is the best way to learn java
What is the best way to learn JavaScript?
What is the best way to learn JavaScript?
Best way to reading file in java
Read from file java
Read file in java
java read file
Read and write file
Read external file in PHP
Java read binary file
Read Video File
Read the file Contents
Read text File
read restricted pdf file
read restricted pdf file
javaa swings - IDE Questions
fastest type of JDBC driver
What is an efficient way to implement a singleton pattern in Java?
What are some way to learn Java quickly?
Javap Tool application
What is the best way to learn spring boot
What is the best way to learn spring boot
What's the best way to learn data science as a beginner?
What is the best way to create a string array in python
What is the best way to filter a Java Collection?
What is the best way to learn Java on my own?
Maven dependency for com.gooddata - gooddata-http-client version 1.0.0+java7 is released. Learn to use gooddata-http-client version 1.0.0+java7 in Maven based Java projects
What is a JAR file in Java
J2ME Read Multiple File - MobileApplications
nio read file line by line

Ads