簡體   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