[英]Storing contents of file in ArrayList of Integer using BufferedReader
我想存儲以下格式的文本文件的內容:
第一行包含 2 個整數,比如 n 和 k,用空格分隔。
以下行包含 n 個整數(n 可以非常大)(我想將其存儲在 ArrayList 中)
通過在線搜索 Java 中處理文件的方式,我了解到 BufferedReader 是處理大輸入時的首選方式,因為它更快(與 Scanner 相比)以及如何做到這一點。 我的嘗試如下:
public static void main(String[] args) throws IOException {
StringBuilder t = new StringBuilder();
try(BufferedReader br = Files.newBufferedReader(Paths.get("./interview1.txt"))) {
t.append(br.readLine());
String[] firstLine = new String[2];
firstLine = t.toString().split(" ");
int n = Integer.parseInt(firstLine[0]);
// System.out.println(n);
int k = Integer.parseInt(firstLine[1]);
// System.out.println(k);
String line;
List<Integer> list = new ArrayList<>(n);
while((line = br.readLine()) != null){
String[] thisLine = line.split(" ");
for(int i = 0; i < thisLine.length; i++){
// System.out.print(thisLine[i] + " ");
list.add(Integer.valueOf(thisLine[i]));
}
}
} catch (Exception e) {
System.err.format("IOException: %s%n", e);
}
因為我知道第一行只包含 2 個數字,所以我讀取了該行,將其轉換為大小為 2 的字符串數組,然后將 2 個整數分開。 到目前為止一切都很好。
問題來了,進入while循環后。 在嘗試將內容添加到列表中時,我不斷收到 java.lang.NumberFormatException (據我所知,這意味着我嘗試將不包含可解析 int 的 String 轉換為 Integer)。
取消對 for 循環中第一行的注釋會產生以下輸出:
3 4 2 1 6 7 1 2 2 3 (extra random 2 towards the end??)
當前使用的文件內容:
10 5
3 4 2 1 6 7 1 2 3
我究竟做錯了什么?
您似乎可以使用java.nio
,所以我建議盡可能多地使用它的功能並擺脫BufferedReader
:
我的示例文件numbers.txt如下所示:
1 2 14 55 165
2
3 4 5 6 7
4
6
7 8 9 10 11 12 13 14 15 16 17
你可以閱讀和數字存儲在不同的List<Integer>
為在每行List<List<Integer>>
或只儲存所有你在一個單一的該文件讀取的數字List<Integer>
檢查以下代碼及其注釋:
public static void main(String[] args) {
// provide the path to the file
Path pathToFile = Paths.get("Y:\\our\\path\\to\\numbers.txt");
// provide a list of lists of numbers
List<List<Integer>> numbersInLines = new ArrayList<>();
// or provide a single list of integers
List<Integer> allNumbers = new ArrayList<>();
// try reading the file, you may want to check if it exists and is valid before
try {
Files.lines(pathToFile).forEach(line -> {
// provide a list for the numbers of each line
List<Integer> realNumbers = new ArrayList<>();
// split the line by an arbitrary amount of whitespaces
String[] lineElements = line.trim().split("\\s+");
// convert them to integers and store them in the list of integers
for (String lineElement : lineElements) {
// check if there is an empty String
if (!"".equals(lineElement)) {
// add it to the list of the current line
realNumbers.add(Integer.valueOf(lineElement));
// and / or add it to the list of all numbers
allNumbers.add(Integer.valueOf(lineElement));
}
}
// store the list of numbers of the current line in the list of lists
numbersInLines.add(realNumbers);
});
} catch (IOException e) {
e.printStackTrace();
}
// print each line list
numbersInLines.forEach(lineNumbers -> {
lineNumbers.forEach(System.out::print);
System.out.println();
});
// and / or print all the numbers in a single line, separated by a whitespace
allNumbers.forEach(number -> System.out.print(number + " "));
}
輸出將是這樣的:
121455165
2
34567
4
6
7891011121314151617
1 2 14 55 165 2 3 4 5 6 7 4 6 7 8 9 10 11 12 13 14 15 16 17
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.