java - read file line by line in Java

Ads

 
 

Share on Google+Share on Google+

Java Coder
java - read file line by line in Java
4 Answer(s)      2 years and 7 months ago
Posted in : Java Beginners

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

Ads
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


Ads









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... write() adds the simple text to the file. Then the nextLine() writes a line
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
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
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
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
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
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
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
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
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 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
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
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 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 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... command line arguments in java program. */ class CmndLineArguments { public
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
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
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
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
Command Line Arguments in Java Program
Command Line Arguments in Java Program       Java application can accept any number of arguments... command line arguments like: java CmndLineArguments Mahendra zero one two
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
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
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... command line: View more tutorials on Java
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