簡體   English   中英

從文本文件中讀取整數並將它們存儲到 Java 中的數組中

[英]Read integers from a text file and store them into an array in Java

所以我有一個帶有整數的文本文件,如下所示:

20 25 3 239 6 184 211 155 245 25 13 73 73 82 70 164 164 102 193 44 205 250 145 102 95 83 152 168 148 193 54 228 86 244 10 26 181 106 53 209 249 21 150 213 92 234 135 121 54 8 241 252 68 169 165 159 182 56 58 158 72 15 19 10

如何讀取具有上述整數的文件並將它們存儲到以 20 開頭並以 10 結尾的數組中。

您可以使用 java 的文件 nio.file api 將文件數據讀入字符串,然后使用空格將該字符串拆分為 String[] 並將每個數字從 String 轉換為該數組中的 int。

String fileData = new String(Files.readAllBytes(Path.of("file path here")));
int[] data = Stream.of(fileData.split(" ")).mapToInt(Integer::parseInt).toArray();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM