【问题标题】:How to save a 2D array into a text file with BufferedWriter?如何使用 BufferedWriter 将二维数组保存到文本文件中?
【发布时间】:2016-01-23 02:09:07
【问题描述】:

我正在尝试使用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) {

}

【问题讨论】:

  • 建议:不要沉迷于文件的物理外观。考虑存储为逗号分隔的列表,如<rows>, <cols>, val1, val2, val3... valn

标签: java multidimensional-array bufferedreader bufferedwriter


【解决方案1】:

我建议(就像 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();

【讨论】:

  • 非常干净,先生。 BufferedWriter 有效。现在你将如何使用BufferedReader 将我们刚刚所做的反转回数组?我会把你标记为答案。谢谢。
  • 对不起,我忘记了第二部分 xD 我现在才编辑我的答案,因为我回答的时候是凌晨 4 点,然后我就睡觉了:S 对不起:D
  • 哈哈我知道那些漫长的夜晚!尊重:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 2017-04-11
  • 1970-01-01
  • 2016-10-21
  • 2021-05-18
  • 1970-01-01
相关资源
最近更新 更多