返回.txt文件中的行数

时间:2021-03-19 20:20:06

This is my debut question here, so I will try to be as clear as I can.

这是我的首发问题,所以我会尽量清楚。

I have a sentences.txt file like this:

我有一个这样的sentences.txt文件:

Galatasaray beat Juventus 1-0 last night.

加拉塔萨雷昨晚1-0击败尤文图斯队。

I'm going to go wherever you never can find me.

我要去任何你从未找到我的地方。

Papaya is such a delicious thing to eat!

木瓜是如此美味的食物!

Damn lecturer never gives more than 70.

该死的讲师永远不会超过70。

What's in your mind?

你在想什么?

As obvious there are 5 sentences, and my objective is to write a listSize method that returns the number of sentences listed here.

很明显有5个句子,我的目标是编写一个listSize方法,返回此处列出的句子数。

public int listSize()
{
// the code is supposed to be here.

return sentence_total;}

All help is appreciated.

所有帮助表示赞赏。

4 个解决方案

#1


2  

To read a file and count its lines, use a java.io.LineNumberReader, plugged on top of a FileReader. Call readLine() on it until it returns null, then getLineNumber() to know the last line number, and you're done !

要读取文件并计算其行数,请使用插入FileReader之上的java.io.LineNumberReader。在它上面调用readLine()直到它返回null,然后getLineNumber()知道最后一个行号,你就完成了!

Alternatively (Java 7+), you can use the NIO2 Files class to fully read the file at once into a List<String>, then return the size of that list.

或者(Java 7+),您可以使用NIO2 Files类将文件一次完全读入List ,然后返回该列表的大小。

BTW, I don't understand why your method takes that int as a parameter, it it's supposed to be the value to compute and return ?

顺便说一句,我不明白为什么你的方法将int作为参数,它应该是计算和返回的值?

#2


1  

Using LineNumberReader:

LineNumberReader  reader = new LineNumberReader(new FileReader(new File("sentences.txt")));
reader.skip(Long.MAX_VALUE);
System.out.println(reader.getLineNumber() + 1); // +1 because line index starts at 0
reader.close();

#3


0  

use the following code to get number of lines in that file..

使用以下代码获取该文件中的行数。

    try {
        File file = new File("filePath");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        int totalLines = 0;
        while((line = reader.readLine()) != null) {
            totalLines++;
        }
        reader.close();
        System.out.println(totalLines);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }

#4


0  

You could do:

你可以这样做:

Path file = Paths.getPath("route/to/myFile.txt");
int numLines = Files.readAllLlines(file).size();

If you want to limit them or process them lazily:

如果你想限制它们或懒惰地处理它们:

Path file = Paths.getPath("route/to/myFile.txt");
int numLines = Files.llines(file).limit(maxLines).collect(Collectors.counting...);

#1


2  

To read a file and count its lines, use a java.io.LineNumberReader, plugged on top of a FileReader. Call readLine() on it until it returns null, then getLineNumber() to know the last line number, and you're done !

要读取文件并计算其行数,请使用插入FileReader之上的java.io.LineNumberReader。在它上面调用readLine()直到它返回null,然后getLineNumber()知道最后一个行号,你就完成了!

Alternatively (Java 7+), you can use the NIO2 Files class to fully read the file at once into a List<String>, then return the size of that list.

或者(Java 7+),您可以使用NIO2 Files类将文件一次完全读入List ,然后返回该列表的大小。

BTW, I don't understand why your method takes that int as a parameter, it it's supposed to be the value to compute and return ?

顺便说一句,我不明白为什么你的方法将int作为参数,它应该是计算和返回的值?

#2


1  

Using LineNumberReader:

LineNumberReader  reader = new LineNumberReader(new FileReader(new File("sentences.txt")));
reader.skip(Long.MAX_VALUE);
System.out.println(reader.getLineNumber() + 1); // +1 because line index starts at 0
reader.close();

#3


0  

use the following code to get number of lines in that file..

使用以下代码获取该文件中的行数。

    try {
        File file = new File("filePath");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        int totalLines = 0;
        while((line = reader.readLine()) != null) {
            totalLines++;
        }
        reader.close();
        System.out.println(totalLines);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }

#4


0  

You could do:

你可以这样做:

Path file = Paths.getPath("route/to/myFile.txt");
int numLines = Files.readAllLlines(file).size();

If you want to limit them or process them lazily:

如果你想限制它们或懒惰地处理它们:

Path file = Paths.getPath("route/to/myFile.txt");
int numLines = Files.llines(file).limit(maxLines).collect(Collectors.counting...);