在 java 中读取文件并将每个元素保存到 2 个数组中时遇到问题。我的txt是这样制作的
2,3
5
4
2
3
1
其中第一行是两个数组 A=2 和 B=3 的长度,然后是每个数组的元素。我不知道如何将它们保存到 A 和 B 中并用它们的长度初始化数组。最后每个数组将是 A=[5,4] B=[2,3,1]
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("prova.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != " ") {
String[] delims = strLine.split(",");
String m = delims[0];
String n = delims[1];
System.out.println("First word: "+m);
System.out.println("First word: "+n);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
这就是我所做的..我使用 System.out.println....只是在控制台中打印它没有必要...有人可以帮助我,给我一些建议吗?提前致谢