如何用BufferedWriter将二维数组保存为文本文件?

How to save a 2D array into a text file with BufferedWriter?(如何用BufferedWriter将二维数组保存为文本文件?)

本文介绍了如何用BufferedWriter将二维数组保存为文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BufferedWriter将2D数组存储在文本文件中,我还希望使用BufferedReader从文本文件中检索2D数组并以其原始数组格式显示。这两种方法我都没什么经验。

要保存在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) {

}

推荐答案

我建议(和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("
");//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将二维数组保存为文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何用BufferedWriter将二维数组保存为文本文件?

基础教程推荐