2

Below is how i count the number of lines in a text file. Just wondering is there any other methods of doing this?

while(inputFile.hasNext()) {    
    a++;
    inputFile.nextLine();
}
inputFile.close();

I'm trying to input data into an array, i don't want to read the text file twice.

any help/suggestions is appreciated.

thanks

Madhawa Priyashantha
  • 9,208
  • 7
  • 28
  • 58
Nghia
  • 43
  • 2
  • 2
  • 5
  • refer http://stackoverflow.com/questions/1277880/how-can-i-get-the-count-of-line-in-a-file-in-an-efficient-way – DeepInJava Oct 19 '14 at 07:33
  • Do you want to count the lines or store then in an array? If the latter you don't need to count the lines... – assylias Oct 19 '14 at 07:34
  • Yeah. I had to read the text then make an array based on the length of the text. then read the data again and put it into the array. its like 10-15 lines of code alone to just count the text file. – Nghia Oct 19 '14 at 07:35
  • Thanks for that link Sanket! that's really helpful – Nghia Oct 19 '14 at 07:35

5 Answers5

7

If you are using java 7 or higher version you can directly read all the lines to a List using readAllLines method. That would be easy

readAllLines

List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());

Then the size of the list will return you number of lines in the file

int noOfLines = lines.size();
5

If you are using Java 8 you can use streams :

long count = Files.lines(Paths.get(filename)).count();

This will have good performances and is really expressive.

The downside (compared to Thusitha Thilina Dayaratn answer) is that you only have the line count. If you also want to have the lines in a List, you can do (still using Java 8 streams) :

// First, read the lines
List<String> lines = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Then get the line count
long count = lines.size();
superbob
  • 1,478
  • 12
  • 23
2

If you just want to add the data to an array, then I append the new values to an array. If the amount of data you are reading isn't large and you don't need to do it often that should be fine. I use something like this, as given in this answer: Reading a plain text file in Java

BufferedReader fileReader = new BufferedReader(new FileReader("path/to/file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} finally {
    br.close();
}

If you are reading in numbers, the strings can be converted to numbers, say for integers intValue = Integer.parseInt(text)

Community
  • 1
  • 1
Edgar H
  • 1,032
  • 1
  • 16
  • 27
0

I do not have enough reputation to comment but @superbob answer is almost perfect, indeed you must ensure to pass Charset.defaultCharset() as 2nd parameter like :

Files.lines(file.toPath(), Charset.defaultCharset()).count()

That's because Files.lines used UTF-8 by default and then using as it is on non default UTF-8 system can produce java.nio.charset.MalformedInputException.

S. Ktifa
  • 121
  • 1
  • 9
  • *"I don't have enough reputation to comment"* **does not mean that you should use an Answer instead**. It means that you need to learn how to use the site, and when you are sufficiently experienced, you will be granted the [privilege](/help/privileges) of commenting. Until then, please [provide answers that don't require clarification from the asker](//meta.stackexchange.com/q/214173). – Toby Speight Nov 17 '16 at 16:04
  • Woah, that's nice of you... I just wanted to give extra informations to people reading that page, and that's the only way I could've think of, at this moment. If that was a bad idea/behavior, I'm sorry I only wanted to help... – S. Ktifa Nov 18 '16 at 15:07
-1

You can do it just like this, everyone has some complicated way of doing it when you can just simply do it like this:

$file = "file.txt";
$count = count(file($file));  
echo "there are $count lines";

Very easy to use.

Crimin4L
  • 312
  • 3
  • 13
  • 1
    that is not java, or is it a feature I did not know exist ? – Leo Apr 06 '20 at 01:06
  • Ooops, sorry thought this was php, not java :L Couldn't you use BufferedReader to do this process? Haven't use java in 8+ years but I think I would use a BufferedReader to read the file and then count the lines. Sorry if I am incorrect :P – Crimin4L Apr 08 '20 at 20:10