我将尝试使用
所需保存在txt文件中的结果如下:
BufferedWriter
将二维数组存储到文本文件中,也希望使用BufferedReader
从文本文件中检索并以其原始数组格式显示二维数组。我对这两种方法都缺乏经验。所需保存在txt文件中的结果如下:
1 5 7 8 2 3 9 6 4
4 8 3 7 6 9 2 1 5
6 2 9 5 1 4 7 3 8
5 3 1 9 4 2 6 8 7
2 7 4 3 8 6 5 9 1
8 9 6 1 7 5 3 4 2
9 4 8 2 3 7 1 5 6
3 6 2 4 5 1 8 7 9
7 1 5 6 9 8 4 2 3
BufferedWriter:
// Instantiate a Date object
static Date date = new Date();
static int[][] board = {{0, 0, 5, 9, 7, 1, 8, 4, 6},
{0, 7, 1, 2, 8, 6, 9, 3, 5},
{0, 9, 6, 4, 3, 5, 2, 7, 1},
{0, 6, 8, 7, 4, 9, 5, 2, 3},
{0, 4, 9, 5, 2, 3, 1, 6, 8},
{0, 5, 2, 1, 6, 8, 4, 9, 7},
{0, 2, 4, 8, 1, 7, 3, 5, 9},
{0, 1, 3, 6, 5, 2, 7, 8, 4},
{0, 8, 7, 3, 9, 4, 6, 1, 2}};
try {
File file = new File("/c:/sudoku" + date + ".txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(board, 0, board.length());
}
System.out.println("Your Game was saved with success !");
} catch (IOException e) {
}
<行数>,<列数>,值1,值2,值3... 值n
。 - Bob Kaufman