• Register
1 vote
30 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,260 points

Related questions

0 votes
1 answer 24 views
24 views asked Jan 25 ahmedadel9090 3.1k points
0 votes
1 answer 16 views
16 views
I was wondering if there is a smarter way to do that
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: I need someone to understand this problem and find out the solution. How to get the number of lines in a file c++
asked Apr 25 Ifra 36.4k points
1 vote
1 answer 2 views
2 views
Problem: Why I got the error : python get number of lines in file
asked Mar 20 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 18 views
18 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 Feb 20 Mashhoodch 13k points
1 vote
1 answer 29 views
29 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 92 views
92 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
0 answers 4 views
4 views
Problem: Do you have any suggestions about how I can resolve this “Which utility gives the number of lines, words, and characters in the contents of a file”?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 6 views
6 views
Problem: Every time I open my Command prompt it flashes the below message and then closes immediately. the screen cannot be set to number of lines and column specified What is the problem? How can I solve it?
asked 6 days ago jamuna1 30.2k points