如何使用BufferedWriter将2D数组保存到文本文件中?

时间:2016-01-23 02:09:07

标签: java multidimensional-array bufferedreader bufferedwriter

我正在尝试使用BufferedWriter将2D数组存储在文本文件中,我还想从文本文件中检索2D数组,并使用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 个答案:

答案 0 :(得分:6)

我建议(正如Bob所做的那样)以csv格式存储(逗号分隔值)

Date date = new Date();
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}}; 

StringBuilder builder = new StringBuilder();
for(int i = 0; i < board.length; i++)//for each row
{
   for(int j = 0; j < board.length; j++)//for each column
   {
      builder.append(board[i][j]+"");//append to the output string
      if(j < board.length - 1)//if this is not the last row element
         builder.append(",");//then add comma (if you don't like commas you can use spaces)
   }
   builder.append("\n");//append new line at the end of the row
}
BufferedWriter writer = new BufferedWriter(new FileWriter("/c:/sudoku" + date + ".txt"));
writer.write(builder.toString());//save the string representation of the board
writer.close();

希望一切都清楚

编辑: 以下是如何阅读您的电路板:

String savedGameFile = /*...*/;
int[][] board = new int[9][9];
BufferedReader reader = new BufferedReader(new FileReader(savedGameFile));
String line = "";
int row = 0;
while((line = reader.readLine()) != null)
{
   String[] cols = line.split(","); //note that if you have used space as separator you have to split on " "
   int col = 0;
   for(String  c : cols)
   {
      board[row][col] = Integer.parseInt(c);
      col++;
   }
   row++;
}
reader.close();