MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create, write and count lines in a file with Java

In this article, we will create a file and write some content in it and then we will count the number of lines that it contains.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

In this article we will learn :

  1. Make a file with some content in it
  2. Count the number of line with program

First we will discuss to store data in file:

Listing1: Storing data in File

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Storage {
	FileWriter fw;
	BufferedWriter bw;
		public static void main(String args[]){
			Storage s=new Storage();
			
		}
		public void start(String id){

			try{
				fw=new FileWriter("out.txt",true);
				bw=new BufferedWriter(fw);	
				bw.write("This is text number 1");
				bw.write("This is text number 2");
				bw.write("This is text number 3");
				bw.write("This is text number 4");
				bw.write("This is text number 5");
				bw.newLine();
				bw.close();
			}catch(Exception e){
				e.printStackTrace();
				System.out.println("error"+e.getMessage());
			}
		}
} 

Here:

  1. We define a class which is named as storage
  2. We place a loop which run a function start.
  3. Now in start method we make an object of FileWriter and BufferedWriter object and pass out.txt as an argument. This way we tell the program that we are going to write in out.txt file
  4. Now we use the write method to write to the out.txt file.
  5. After we complete writing we close the BufferedWriter object.This is necessary since when this is closed then only the content is saved in the file.
  6. We placed this all in try-catch so that any exception is caught and the reason for error is shown to user on console

OUTPUT :

This is text number ID 1
This is text number ID 2
This is text number ID 3
This is text number ID 4
This is text number ID 5

Now we will discuss how we may count the number of lines in the file. We just made by above program.

Listing 2: Count lines in file

import java.io.BufferedReader;
import java.io.FileReader;

public class CountLine {
	public static void main(String args[])	{
		CountLine s=new CountLine();
			s.start();
	}
		public void start()	{

			try{
				FileReader fr=new FileReader("out.txt");
				BufferedReader br=new BufferedReader(fr);
				String line=br.readLine();
				int count=0;
					while(line!=null){
						count++;
						line=br.readLine();				
					}
				System.out.print("Total number of lines"+count);
				br.close();
			}catch(Exception e){
				e.printStackTrace();
				System.out.println("error"+e.getMessage());
			}
		}
} 

OUTPUT

Total number of lines5

Here:

  1. We define a class name CountLine
  2. In the main method we call start method of this class
  3. Now in the start method we define FileReader and BufferedReader object. These will be used to read the file
  4. Now we use out.txt file we created earlier and pass the too FileReader and BufferedReader object.
  5. We define a string variable line and initialize with a line read from input file. We define another variable which is count. It is used for counting the total number of lines in that file
  6. Now we make a loop which runs until we reach the end of file. For this we will use the help of line variable which is updated every time with a new line in the input file
  7. We increment the counter since the program comes only inside loop when a line has been read from the file
  8. We update the line variable to the content of next line and the loop continues until the file end reaches where line variable starts containing null
  9. After that we dislay the number of lines we found in the console
  10. We put this whole thing inside a try-catch so that any exception can be caught and is handled properly
  11. If any exception is caught control comes to catch and a proper error message is displayed on console screen

Conclusion

Line count operations are frequently needed where we are writing out data onto simple files. Like we are writing several url onto file and we want to know a total of all url within the file then we may use the program

This is all for today’s article. Hope you liked it. See you next time with some more interesting articles



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login