Scanner Class to Read Text file

Scanner Class to Read Text file

Hi,

I want to learn the use of Scanner Class to Read Text file. I have large text file that my company told me to process.

What will the efficient for processing such a big size data?

Thanks

View Answers

November 27, 2024 at 4:59 AM

Hi,

Reading big file in memory is not an efficient method. You have to read the file one line at at time and process.

Here is complete code in Java that reads a text file "data.txt" one line at a time. In this example we have just printed the content on the console, but in real business scenario you can write code to process one line at a time.

Example code of Scanner Class to Read Text file:

package net.roseindia;
import java.io.File;
import java.util.Scanner;

/**
 * In this example we are going to use Scanner class to read a text file
 * Web: https://www.roseindia.net
 */

public class ScannerReadTextFile {

    public static void main(String[] args) {
        String fileName = "data.txt";
        try {
            File file = new File(fileName);

            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                String line = input.nextLine();
                System.out.println(line);
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Hope this helps you.

Thanks









Related Tutorials/Questions & Answers:
how to read a text file with scanner in java
class. how to read a text file with scanner in java? Thanks   Hi...how to read a text file with scanner in java  Hi, I am looking... String fileName = "data.txt"; // Read file with Scanner class
Java read text file
to Read file line by line is by using Scanner Class. The benefit of using Scanner... a text file one line at a time. It can also be used to read large text files... text file in Java?": ADS_TO_REPLACE_3 Example of Read text File Line
Advertisements
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
Java file scanner
Java file scanner In this section, you will learn how to read a file using Scanner class. Description of code: J2SE5.0 provides some additional classes... input-output stream, we have used Scanner class to read the file.ADS_TO_REPLACE_2
Steps to read text file in pyspark
Steps to read text file in pyspark  Hi, I am learning to write program in PySpark. I want to simply read a text file in Pyspark and then try some code. What are the Steps to read text file in pyspark? How much time it takes
Read Lines from text file
read from the text file and displays the output as desired. Unable to read the rest...Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each
How to read text file in Servlets
How to read text file in Servlets  ... file in servlets. In this example we will use the input stream to read the text from the disk file. The InputStreamReader class is used to read the file
read text file and store the data in mysql - JDBC
read text file and store the data in mysql  when we store the data in mysql table from text file its store the data from new line to new column. how to store the data in different column from a single line of text file
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
Read text file in PySpark
Read text file in PySpark - How to read a text file in PySpark? The PySpark...().setAppName("read text file in pyspark") sc = SparkContext(conf=conf... configuration conf = SparkConf().setAppName("read text file in pyspark") sc
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file... want to displaythe above .txt file in jtable as following format having 3 columns contigID length size and then display sequence like "ATGCGSA..." in text
How to Read file line by line in Java program
. Example of Read file line by line using Scanner Class: package ReadFile...Programmers have always found it difficult to read a big file (5-10 GB) line by line in Java. But there are various ways that can help read a larger file
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  ...*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane... java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1
Scanner class
Scanner class  import java.util.*; class Dd { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int... Scanner class was introduced in java 5
Creating a File I/O using import scanner, io.* and text.*
Creating a File I/O using import scanner, io.* and text.*  open a file for reading and open another file for writing data. input file hours-int, hourly rate-double, name-string. output file name-string, hours-int, hourly rate
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating.... Our requirement is to read a text file and then write the content of the text... with the byte input stream and the class is used to read data from a file. The could
Scanner class
Scanner class  what have to do when an error occur about Scanner class.i code scanner sc=new Scanner(System.in); but it shows an error regarding this.   Use Scanner sc=new Scanner(System.in
how to read text file with java 8 stream api
how to read text file with java 8 stream api  Hi, I want to use Java 8 Stream API for reading text file line by line. I am trying to find example code. how to read text file with java 8 stream api? Thanks   Hi
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji 34,23,56 this is the student marks in text file.this data read and calculate
Read user input in Java using Scanner
are explaining the use of Scanner class in Java which can be used to read user... input is read using the scanner class. Information entered by user is printed... and Scanner class is one of them. Scanner class can be used to read user input from
Java read file
There are many ways to read a file in Java. DataInputStream class is used to read text File line by line. BufferedReader is also used to read a file in Java... BufferedReader BufferedReader class read text from a character-input stream rather
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been assigned a work to read big text file and extract the data and save into database... memory assigned is also a limit. So, we have decided to read the text file line
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text File  I need to read a file that was selected by the user using... to another text file. When that is done the output values of that file need
Read text file to 2D array and sorting the second column
Read text file to 2D array and sorting the second column  we found that the student names with marks in a text file the marks decreases significantly... their names in a text file
Line by Line reading from a file using Scanner Class
In this section, you will get to know about reading line by line a text file using java.util.Scanner class
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Java Read File Line by Line - Java Tutorial
class to Read text File Line by Line. Our example program is reading a text... Java Read File Line by Line - Example code of reading the text file... how to read a text file in Java one line at a time: Video Tutorial
Read File from specified path in Java
: You can also read the file using the Scanner class of Java. Here is the code... the file, the program will read text data line by line and print on the console... file. For simplicity we will read the content of a text file one line at a time
read a file
read a file  read a file byte by byte   import java.io.File; import java.io.FileInputStream; public class ReadFileByteArray {ADS_TO_REPLACE_1 public static void main(String[] args) { File file = new File("D
How to read and compare content of two different text file
Description: In the given example you will see how a two text file's content are compared. The BufferedReader class allow us to read a file. The readLine() method used to read the contents of the specified file.  The way
scanner Class Error - Java Beginners
scanner Class Error  Hello Sir ,When i run the program of Scanner Class ,there is Error Can not Resolve Symbol-Scanner how i can solve..., Scanner class is not provided. Check your version.It should work with java 1.5
file read
file read  hi i am reaing from a file which has punjabi words. can some one help with me some code
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained... java.util.Scanner; class HardwareItems { String code; String description; double
file class
file class  Hi Friend, I have a file class it lets me extract all the data from a csv file but I need to align the data properely, seperating... File"); } Scanner in = new Scanner(System.in); try
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
Read Specific Line from file Using Java
Read Specific Line from file Using Java Here we are going to read a specific line from the text file. For this we have created a for loop to read lines 1 to 10 from the text file. If the loop reached fifth line, the br.readLine() method
class file
class file  How to create class file in jsf easily
How to read file in java
and writing operations with a file. In this section you will learn how to read a text... the given file name. read()-The read() method of FileInputStream class reads... in a machine-independent way.  BufferedReader-This class read the text
class file
class file  how to convert java classfile into java sourcefile
What is the fastest way to read a file in Java?
have to read the text file of big size you should use the BufferedReader class... file using the Scanner class. My code is below: import java.io.*; import... have to read text file of 10GB and then process the lines for getting the data
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice...(file); fw= new FileWriter(file); sc = new Scanner(fr... i want to update the quantity in my text file, if the item i entered matches
how to write and read text for javaME
how to write and read text for javaME  Hi. I have tried ur read/write coding but why i didnt get the o/p just like urs. do i have to add anything from the library? i want to type multiple line on text file then, read it from
Read the File
Read the File       As we have read about  the BufferedInputStream class...:\nisha>java ReadFilter This is a text file C:\nisha>
Read the file Contents
Read the file Contents  Ravi Raj,Vijay45Shankar,234 Guna,345,Meet me,654,Cow Read file contents and Print the no.of words and Numbers./p> Thanks, Dinesh Ram   The given code read the file content and print
read restricted pdf file
read restricted pdf file  i have restricted file. package Encryption; import java.io.*; import java.util.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class EncryptionWithCertificate { public
read restricted pdf file
read restricted pdf file  i have restricted file. package Encryption; import java.io.*; import java.util.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class EncryptionWithCertificate

Ads