从文本文件中读取整数并将它们存储到 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 将文件数据读入字符串,然后使用 space 将该字符串拆分为 String[] 并转换每个从 String 到此数组中的 int 的数字。
String fileData = new String(Files.readAllBytes(Path.of("file path here")));
int[] data = Stream.of(fileData.split(" ")).mapToInt(Integer::parseInt).toArray();
所以我有一个包含整数的文本文件,如下所示:
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 将文件数据读入字符串,然后使用 space 将该字符串拆分为 String[] 并转换每个从 String 到此数组中的 int 的数字。
String fileData = new String(Files.readAllBytes(Path.of("file path here")));
int[] data = Stream.of(fileData.split(" ")).mapToInt(Integer::parseInt).toArray();