数组写入文件和文件读取数组
//写入文件
File file=new File("data.txt");
FileWriter out=new FileWriter(file);
out.write((num+1)+"
");
for(int i=0;i<sparseArray.length;i++) {
for(int j=0;j<sparseArray[i].length;j++) {
out.write(sparseArray[i][j]+" ");
}
out.write("
");
}
out.close();
System.out.println("--------------------------");
//从文件中读取稀疏数组
BufferedReader in=new BufferedReader(new FileReader(file));
int lines=Integer.parseInt(in.readLine());
int arr2[][]=new int[lines][3];
String line;
String[] temp=null;
int row=0;
while((line=in.readLine())!=null) {
temp=line.split(" ");
for(int j=0;j<temp.length;j++) {
arr2[row][j]=Integer.parseInt(temp[j]);
}
row++;
}
in.close();
System.out.println("从文件中读取的数组:");
for(int[] r:arr2) {
for(int data:r) {
System.out.print(data+" ");
}
System.out.println();
}
System.out.println("-------------------------");