繁体   English   中英

nextLine() 仅读取文本文件的第一行 [关闭]

[英]nextLine() only reading the first line of the text file [closed]

在此处输入图像描述

我正在尝试读取一个文本文件并将其存储在一个数组中,但 nextLine() 似乎只读取文本的第一行。 我正在使用 nextLine() 因为我需要存储单词及其行号。

这是执行此操作的基本示例。

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

public class Main {

    public static void main(String[] args) {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(
                    "/Users/me/Downloads/myfile.txt"));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                // read next line
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM