文件导出:
File file = new File("D:/beyondSparseArray.data");
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
System.out.println();
for (int i = 0; i < chessArr2.length; i++) {
System.out.println();
if (i == chessArr2.length - 1) {
writer.append(chessArr2[i][0] + "," + chessArr2[i][1] + "," + chessArr2[i][2]);
} else {
writer.append(chessArr2[i][0] + "," + chessArr2[i][1] + "," + chessArr2[i][2] + ",");
}
}
System.out.println("写入文件中~~");
writer.close();
fileOutputStream.close();
文件读取
System.out.println("打开文件中...");
Desktop.getDesktop().open(file);
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
}
System.out.println(sb.toString());
reader.close();
fis.close();
System.out.println("-----恢复成数组 sparseArrHf(3列的二维数组) ");
String[] str = sb.toString().split(",");
int sparseArrHf[][] = new int[str.length / 3][3];
int i = 0;
for (String s : str) {
sparseArrHf[(i - (i % 3)) / 3][i % 3] = Integer.parseInt(s);
i++;
}
转载请注明原文地址:https://blackberry.8miu.com/read-46606.html