• Register
1 vote
20 views

Problem:

Hello Kodlogers,
I have started learning Java programming recently. I am solving problems from many websites and even textbooks. Right now I got a problem that says to get numbers of lines in a file using java program. I don’t know how to solve this type of problem. Literally no idea! I generally ask for codes but this time I have no other ways. So, it would be better if you can solve the problem and attach the code for my better understanding.

Thanks for your concern.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

To read a file we have a built-in class in Java. import java.io.FileReader is the class who takes input files from HDD and import java.io.LineNumberReader class helps to read the number of a file imported. So, first of all, we have to add these class on the top of our program. Let’s jump into the program directly.

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class MyClass{
    public static void main(String[] args){	
    	try{
    		File myfile =new File("D:\\my_text.txt");
    		if(myfile.exists()){
    		    FileReader roy = new FileReader(myfile);
    		    LineNumberReader lnr = new LineNumberReader(roy);
    		    int linenumber = 0;
    	            while (lnr.readLine() != null){
    	        	linenumber++;
    	            }
    	            System.out.println("Number of lines in this file: " + linenumber);
    	            lnr.close();
    		}else{
    			 System.out.println("This file does not exists!");
    		}
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    }
}

I hope this program will help you to understand better. Give it a try and let me know if it doesn’t wor for you.
Thanks.

13 9 6
94,240 points

Related questions

0 votes
1 answer 2 views
2 views
Problem: I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file I was wondering if there is a smarter way to do that
asked 3 days ago Ali 4.4k points
0 votes
1 answer 10 views
10 views asked Jan 25 ahmedadel9090 2.6k points
0 votes
1 answer 7 views
7 views
Problem: lines() - read all lines of the file as a stream count() - returns the number of elements in the stream Here, if the file input.txt contains the following content: This is the question on Java Examples. The examples count number of lines in a file. , youhave used the java.nio.file package.
asked 6 days ago Ali 4.4k points
0 votes
1 answer 11 views
11 views
I was wondering if there is a smarter way to do that
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 9 views
9 views
Problem: I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise? At the moment I do: def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 is it possible to do any better?
asked 6 days ago Mashhoodch 9.1k points
1 vote
1 answer 25 views
25 views
Problem: I have a python file that contains too many lines of code. I have written a code to count the total number of lines in a exact file. My code: def file_name(fname): with open(fname) as fn: for i, l in enumerate(fn): pass return i+1 Does the code give me the exact count ? If not how can I solve this?
asked Apr 5, 2020 LizzyM 6.1k points
1 vote
1 answer 34 views
34 views
Problem: I am a new student of c++ and learning new things day by day. Well, I have a text file containing thousands of lines possibly. I am willing to give them a count (if possible). What is the easiest and efficient way to get number of lines in a file c++? Please, keep it simple so that I can understand the code easily. Thanks.
asked Mar 29, 2020 Gavin 15.3k points
1 vote
1 answer 17 views
17 views
Problem: Well, I have a text file containing thousands of lines possibly. I am willing to give them a count (if possible). What is the easiest and efficient way to write a program to get number of lines in a file python? Please, keep it simple so that I can understand the code easily. Thanks.
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 24 views
24 views
Solve this problem.
asked Dec 25, 2020 TeamScript 13.5k points
0 votes
1 answer 37 views
37 views
I am new to this. I just want to know how to do that?
asked Nov 6, 2020 TeamScript 13.5k points