java - read file line by line in Java

java - read file line by line in Java

Hi,

how one can read a text file line by line in Java? What is the benefits of reading a file line by line in Java? Why I can't read a big text file in memory?

Thanks

View Answers

February 14, 2015 at 4:47 PM

A text file in Java can be read line by line with the help of BufferedReader class in Java.

The simple code is:

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 4:51 PM

What is the benefits of reading a file line by line in Java?

Now let's discuss the benefits of reading a file line by line.

Actually you can read a file at once in memory, but there is the limit of the memory. So, you can't read the big file in memory at once due to memory constraints.

So, reading a file line by line is the best option and it takes less memory and the performance of the application is also very good.

So, if there is any requirement of reading a text file in your project then you should always write the code to read a file line by line.

Thanks


February 14, 2015 at 4:54 PM

Why I can't read a big text file in memory?

You can't read the whole content of big file in memory at one go due to memory constraint of the hardware and operating system.

Reading the whole file in memory at one go is also requires more processing power and it may slow down the performance of your application.

Thanks


December 19, 2015 at 2:53 AM

Very good Explanation.Thanks for your valuable sharing.

Java training in chennai









Related Tutorials/Questions & Answers:
How to write file by line in Java
How to write file by line in Java  How to write file by line in Java   Hi, For Write to a file by line using java programming language we... in a new line. How to use Write To File By Line in Java programs
Java write to file line by line
Java write to file line by line In this section, you will learn how to write a file line by line. It is better to use Writer class instead of OutputStream class if you want to output text to the file since the purpose of Writer classes
Advertisements
Java Write To File By Line
Java Write To File By Line In this tutorial you will learn how to write to file by line Write to a file by line using java you can use the newLine() method... how to write to file by line. In this example I have first created a new text
Java file new line
Java file new line In this section, you will learn how to write the text in new line of text file. Description of code: The package java.io.* has provide... a text in the file, there are different output streams. Among them
Java Command Line Input
Java Command Line Input  How to get input from command line in java ?   Hi please Find the code for inputting data from the command line System.out.println("Please Input A Number"); BufferedReader br
java line chart
java line chart  Hi, I want to draw a graphic in java line chart and ? searched the documents ?n the web. I found the example codes ?n your site (http://www.roseindia.net/chartgraphs/xyline-chart.shtml) and tr?ed ?n my
Java Write To File End Of Line
Java Write To File End Of Line In this tutorial you will learn how to write to file at the end of the line. Write to a file in which some contents... at the end of line using java program I have constructed the FileWriter object using
putting words to line number form a java file/test file and show occurrence
putting words to line number form a java file/test file and show occurrence  hi all i want putting words to line number form a java file and show... and BinarySearchTree and please help me in java coding
Line Graphs in java - Java Beginners
Line Graphs in java  Hi... I need to generate a graph by using the database values using java I know about the JFreeChart class... But by using... File("C:/chart.jpg"), chart, 400, 300); } catch (IOException e
What is command line argument in Java
example of command line argument program in Java?? It will be really helpful... visit the following link: http://www.roseindia.net/java/beginners/cmnd-line...://www.roseindia.net/java/beginners/cmnd-line-arguments.shtml
Multi line graph - Java Beginners
Multi line graph  Hi, I want a multi line graph on a single chart using jfree in java.... Can you please let me know the code... thanks in advance  Hi Friend, Loginto the below mentioned URl & download the file
command line arguments - Java Beginners
command line arguments  how can i findout the minimum and maximum no's at the command prompt if i give some values?????????  hi friend, We are providing you a code that will prompt the user to input some numbers
how to write to file at the end of the line
how to write to file at the end of the line using Java program. Plz suggest... will help you for "How to write the file at the end of line using Java language." Please visit this link : how to write to file at the end of the line
line number in a file of entered string
line number in a file of entered string  when i entered string from console , it should show line number in a file of that string
java write to a specific line
java write to a specific line In this tutorial, you will learn how to write at specific line in text file. Java provides java.io package to perform file... of line numbers. It seek to the desired position in the file using the method
Command line argument in java
Command line argument in java. In this section we are going to discuss about command line argument in java. Command line argument allow a user to pass arguments at the time of running the application , after the class name. Java allow
Java Generic program with command line argument
Java Generic program with command line argument  Implement Generic to perform arithmetic evaluation using command line argument by using Java
Draw a line in a JPanel with button click in Java
Draw a line in a JPanel with button click in Java  Draw a line in a JPanel with button click in Java
Command Line Arguments in Java
Command Line Arguments in Java The ongoing Java application can accept any...-line argument is an argument, which passed at the time of running of the Java... /** * How to use command line arguments in java program. */ class CmndLineArguments
How to pass command line arguments in Java?
How to pass command line arguments in Java?  Hi, In a Java program I have to write for reading the command line arguments and use it in a programming. How to pass command line arguments in Java? Share me the sample code
Java memory options from command line
Java memory options from command line  Hi, How to use java memory options from command line? Thanks   Hi, Here is the example code: java -Xms1056m -Xmx1056m MyProgram Thanks
Command Line Standard Output In Java
Command Line Standard Output In Java In this section we will discuss about the Command Line Java IO Standard Output. Standard streams, feature of various O... be written at the console, in a file, or at any output source. Java provides
How to delete line starting with a letter in java Regex?
How to delete line starting with a letter in java Regex?  Hi, i 'm trying to code with regex and java. i want to know how it's possible to delete lines which starts with a letter like G or g in a text. i tried
Java Get Last Line of File
Java Get Last Line of File     ... you in understanding Get Last Line of File in Java . For this we have a class name "Get Last Line of File". Inside the main method we create an instance
Line Animation in Java
Line Animation in Java       In this example we are creating an animated line. The color of  line is changing alternatively. We use drawLine() to draw a line
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
How to write file text In New Line
to write file text in New line in Java... text in new line in Java program. How to write the syntax for this with example... you went new text in new line. For this we created a new text file named
Write Text To File In New Line.
Write Text To File In New Line. In this tutorial you will learn how to write text into file in a new line. When you are writing in a file you may be required to finish the line and want to write the other text in a new line
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
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
Command Line Arguments in Java Program
Command Line Arguments in Java Program   ... command line arguments in java program.    */  ... program with some command line arguments like: java CmndLineArguments
Draw Line in PowerPoint Presentation Using Java
Draw Line in PowerPoint Presentation Using Java       In this example we are going to create line in PowerPoint presentation using java. In this example we are going to make
Compiling and Running Java program from command line
Compiling and Running Java program from command line - Video tutorial... of compiling and running java program from command line. We have also video... of the Compiling and Running Java program from command line: View more tutorials
HOW TO PARSE FILE WITH SEVERAL DELIMITERS AND LINE FEEDERS - Java Beginners
HOW TO PARSE FILE WITH SEVERAL DELIMITERS AND LINE FEEDERS  Hi All i need to parse a file of this format,such that i can get/fetch the values... class Read{ public static void main(String[]args) throws Exception
Sum of two numbers using command line arguments in Java
to pass two values as parameter from command line and the get in it Java program? The calculate the sum of two numbers using command line arguments in Java. Thanks   Hi, While calling Java program from command line you can pass
please tell me about command line arguments in java?
please tell me about command line arguments in java?  please tell me about command line arguments in java?   Hi Friend, The command-line.../java/beginners/cmnd-line-arguments.shtml Thanks
Retriving data from MYSQL without line break using java
Retriving data from MYSQL without line break using java  get data without line breaking from mysql table (i.e data stored as mediumtext )using java
please tell me about command line arguments in java?
please tell me about command line arguments in java?  please tell me about command line arguments in java?   Hi Friend, The command-line... take any number of command-line arguments which may be necessary for the program
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
How can i draw a line using java swings
How can i draw a line using java swings  Sir my program contains different components i am using JFrame. I want to draw a straight line between components(Jtextfeilds, Jlabels).So could you help me for it. Thank You
write a java program to find the summation of all the integers entered on command line
write a java program to find the summation of all the integers entered on command line  Hi, write a java program to find the summation of all the integers entered on command line? How to write Java program for summation of all
Platform dependent values like line separator, path separator - Java Beginners
Platform dependent values like line separator, path separator  Hi, How will you get the platform dependent values like line separator, path...: System.getProperty("line.separator") for line separator System.getProperty
how can add new line character into ajax response for java
how can add new line character into ajax response for java   i want to populated brand drop-down list and state drop-down list by changing on country... down will be in a new line how can i do this i m showing my codes here please
how can add new line character into ajax response for java
how can add new line character into ajax response for java   i want to populated brand drop-down list and state drop-down list by changing on country... down will be in a new line how can i do this i m showing my codes here please
How to insert new line after every space in java
How to insert new line after every space in java  Hii Sir, I have a string with values 69 17 17 16 2 1 1 26 26 56 like this .Now as per my need i have to put all these values into new lines on getting space
Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding
Draw Line
Draw Line  sir i want to draw a moving line in j2me.That line should also show arrow in moving direction. How can we do so
line graph
line graph  I have developed an application and in that i have to compare 2 reports using line graph from the data taken from ms access database?please help
on line exam
on line exam  how we can upload the data on the server from any site

Ads