我有txt文件,其中每行包含兩個詞,例如:讀取文件,掃描儀
USA 321
France 1009
...
Germany 902
我怎樣才能讀取這個文件通過文字在二維數組?我有:
List<List<String>> temps = new ArrayList<>();
Scanner dataScanner = new Scanner(dataFile);
while (dataScanner.hasNextLine()) {
Scanner rowScanner = new Scanner(dataScanner.nextLine());
temps.add(new ArrayList<>(2));
while (rowScanner.hasNextLine()) {
...
}
}
你必須使用掃描儀對該排?如果不是,請使用'String.split()'將行分解成單詞。 –
如果您必須爲該行使用掃描儀,請勿使用'rowScanner.hasNextLine()';它只包含一行。使用'hasNext()'(和'next()')從行中獲取單個單詞。另外,如果你確定每一行總是有2個單詞,你可以使你的結構成爲'List'。 –
DodgyCodeException