我有一个包含以下数据元素的文件:
1749 12426 19597 38042 43350 52873 67704 75875 81829 96307
11615 16454 20015 27021 52762 66631 70625 83951 96527 96893
3370 5530 28692 39087 50591 59442 61906 69337 70643 91162
1947 4604 9657 16455 21149 22739 32809 66089 73871 97304
3429 5325 7888 24101 28851 31637 32424 57991 62470 65017
我面临的问题是在不同的数组索引上添加文件的每个元素。在从互联网上搜索时,我只能将所有第一行存储在索引数组[0]上。 (所有行都存储在索引[0])
我想存储所有列元素数组
array[0] = 1749
array[1] = 11615
array[2] = 3370
array[3] = 1947
array[4] = 3429
第二遍:
array[0] = 12426
array[1] = 16454
array[2] = 5530
array[3] = 4604
array[4] = 5325
等到第10遍..... 请在下面给出的代码中进行必要的更改:
File file = new File("SortedLines.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char)content);
}
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
你应该逐行读取文件,对于每一行,用空格分割,将数字加到数组中:
ArrayList<String> myArray = new ArrayList<String>(0);
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// split by space
String[] numbers= strLine.split(" ");
for(String num numbers){
myArray.add(strLine);
}
}