Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a 2D array into a text file with BufferedWriter?

I'm trying to store a 2D array in a text file with BufferedWriter and I would also like to retrieve a 2D array from a text file and display in its original array format with BufferedReader. I have little experience with both methods.

The desired outcome to be saved in a txt file is:

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) {

}
like image 607
Mark Doe Avatar asked Jan 23 '16 02:01

Mark Doe


1 Answers

I suggest (as Bob did) to store it in csv format (comma separated values)

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();

Hope it's all clear

EDIT: Here is how to read back your board:

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();
like image 61
Tommaso Pasini Avatar answered Sep 18 '22 13:09

Tommaso Pasini