美好的一天, 我需要帮助将文本文件存储到数组中。我是java的新手,但我很难将数据保存在数组中。以下是我到目前为止所做的工作。
public static void main(String[] args )
{
Scanner in = null;
try {
in = new Scanner(new FileReader("input.txt"));
}
catch(IOException e) {
e.printStackTrace();
}
// Get the number of cases
int numCases = Integer.parseInt(in.nextLine());
// Call search on each iteration through the loop
for(int i = 0; i < numCases; i++){
int n = Integer.parseInt(in.nextLine());
String[] tower_height = in.nextLine().split(" ");
for (int i = 0; i < n; i++){
int num_tower[] = new int[n];
in.nextInt();
}
for (int i = 0; i > j; i++){
int tower_height[] = new int[j];
int t_height =interger.parseInt( tower_height[]);
}
}
}
int numTower [] = new int [n];
}
String []height = in.nextLine().split(" ");
tower [] = new int [numTower];
int height[] = new int [num_tower]
int[n] num_towers;
}
for (i = 0; )
for (int i =0; i < n; i++)
tower_height [i] = in.nextInt();
int[]tower_height = new int[n];
for (int j =0; j < num_towers; j++){
;
hight[i] = in.nextInt();
for (int k = 0; k < height; k++){
int span[] = new int [num_tower];
span[k-1] = height[];
**/
}
}
}
}
我的文字文件内容
6
7
100 80 60 70 60 75 85
2
50 60
10
150 140 130 120 110 100 90 80 70 60
10
60 70 80 90 100 110 120 130 140 150
20
60 70 80 90 100 110 120 130 140 150 150 140 130 120 110 100 90 80 70
60
30
245 34 88 90 57 165 227 58 208 129 97 26 133 110 63 127 201 213 225
171 187 74 163 21 172 143 215 66 45 216
答案 0 :(得分:0)
如果您使用的是Java 8,Hector Manuel Martinez Duran给出的答案可以简化:
public List<String> txtToArray(String path)
{
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.lines().toArray();
}
}
}
答案 1 :(得分:0)
List<String> lines = IOUtils.readLines(new FileInputStream("path/of/text"));
将以下依赖性添加到mevan
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>