我已经编写了用于从文本文件传递整数输入的 Java 代码,例如1 10 39 59 20 60 38
,当有空格时我必须拆分字符串。
输入在单行中给出input.txt
我的代码是:
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String calc;
calc = key.toString();
ArrayList<Integer> keys = new ArrayList<Integer>();
String[] data = calc.split(" ");
for (String s : data) {
int intData = Integer.parseInt(s);
keys.add(intData);
}
int val = 0;
for (int a : keys) {
// some tasks
}
}
分割线后,我将分离的值用于不同的任务。我的问题是如何拆分位于同一文件中的所有值(值也在不同的行中)并将它们存储在一个数组中?
假设如果以下是input.txt中给出的输入,那么如何拆分所有值并将它们存储在一个数组中?
示例输入:
1 4 92 58 30 82
49 50 38 30 29 20
...
预期输出:
array1="1,4,92,58,30,82,49,50,38,30,29,20, .."
当我将我的代码用于上述输入时,只考虑输入文件的最后一行 - 所有前面的行都被忽略。