title: Reading and print 2D matrix (array) to console from created File | Java
body: <p>Necessary create a 2D matrix (array) sizes m*b:</p>
  <ol>
  <li>Filled in by manually, found Null, print results</li>
  <li>Filled in by automatically:</li>
  </ol>
  <ul>
  <li>create a File -&gt; record File.txt</li>
  <li>read in created File.txt</li>
  <li>found Null</li>
  <li>print results</li>
  </ul>
  <pre><code>
      package labs1;
      
      import java.io.*;
      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.*;
      
      public class labs2 {
          public static void main(String[] args) throws Exception {
              BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      
              System.out.println(&quot;Add size matrix m*n \nnumber of row m =&quot;);
              int m = Integer.parseInt(reader.readLine());
      
              System.out.println(&quot;number of columns n =&quot;);
              int n = Integer.parseInt(reader.readLine());
      
              int[][] matrix = new int[m][n];
      
              System.out.println(&quot;Choose to method assignments matrix \n0-manually \n1-automatically&quot;);
              int k = Integer.parseInt(reader.readLine());
      
              while (true) {
                  if (k == 0) {
      
                      //
                      for (int i = 0; i &lt; m; i++) {
                          for (int j = 0; j &lt; n; j++) {
                              matrix[i][j] = Integer.parseInt(reader.readLine());
                          }
                      }
                      // print matrix
                      for (int i = 0; i &lt; m; i++) {
                          for (int j = 0; j &lt; n; j++) {
                              System.out.print(matrix[i][j] + &quot; &quot;);
                          }
                          System.out.println(&quot;&quot;);
                      }
      
                      //count of null in matrix
                      int count = 0;
      
                      for (int i = 0; i &lt; matrix.length; i++) {
                          for (int j = 0; j &lt; matrix[i].length; j++)
                              if (matrix[i][j] == 0) {
                                  count++;
                              }
                      }
                      System.out.println(&quot;Quantity of Null in matrix: &quot; + count);
      
                  }
                  //Assignment matrix automatically - Random
                  if (k == 1) {
                      for (int i = 0; i &lt; matrix.length; i++) {
                          // do the for in the row according to the column size
                          for (int j = 0; j &lt; matrix[i].length; j++) {
                              // multiple the random by 10 and then cast to in
                              matrix[i][j] = ((int) (Math.random() * 10));
                          }
                      }
      
                      // print matrix
                      for (int i = 0; i &lt; m; i++) {
                          for (int j = 0; j &lt; n; j++) {
                              System.out.print(matrix[i][j] + &quot; &quot;);
                          }
                          System.out.println(&quot;&quot;);
                      }
      
                      //count of null in matrix
                      int count = 0;
      
                      for (int i = 0; i &lt; matrix.length; i++) {
                          for (int j = 0; j &lt; matrix[i].length; j++)
                              if (matrix[i][j] == 0) {
                                  count++;
                              }
                      }
                      System.out.println(&quot;Quantity of Null in matrix: &quot; + count);
      
      
                      //Create a File .txt
      
                      // String Builder to create the table structure before writing it to the file.
                      StringBuilder sb = new StringBuilder();
      
                      for (int[] int1 : matrix) {
                          for (int j = 0; j &lt; int1.length; j++) {
                              sb.append(int1[j]).append(&quot;\t&quot;);                    // Add tab to delimite the elements
                          }
                          sb.append(&quot;\n&quot;);                                      // Add new line character
                      }
      
                      System.out.println(&quot;\nMatrix record in file\n&quot; + sb);
      
                      // The path to your file
                      Path path = Paths.get(&quot;D:\\labs1.txt&quot;);
      
                      // Writes to that path the bytes in the string from the stringBuilder object.
                      Files.write(path, sb.toString().getBytes());
      
      
                      ArrayList&lt;int[]&gt; list = readMaze(&quot;D:\\labs1.txt&quot;);
      
      
                      System.out.println(Arrays.toString(list.toArray()));
                  }
                  if (k != 0 | k != 1) {
                      break;
                  }
              }
          }
      
          //Reading in File
      
          public static ArrayList&lt;int[]&gt; readMaze(String fileName) {
      
              // Number of ints per line:
              int width = 2;
      
              // This will be the output - a list of rows, each with 'width' entries:
              ArrayList&lt;int[]&gt; results = new ArrayList&lt;int[]&gt;();
      
              String line = null;
      
              try {
                  FileReader fileReader = new FileReader(fileName);
      
                  BufferedReader bufferedReader = new BufferedReader(fileReader);
      
                  Scanner mazeRunner = new Scanner(bufferedReader);
      
                  // While we've got another line..
                  while (mazeRunner.hasNextLine()) {
      
                      // Setup current row:
                      int[] row = new int[width];
      
                      // For each number..
                      for (int i = 0; i &lt; width; i++) {
      
                          // Read the number and add it to the current row:
                          row[i] = mazeRunner.nextInt();
      
                      }
      
                      // Add the row to the results:
                      results.add(row);
      
                      // Go to the next line (optional, but helps deal with erroneous input files):
                      if (mazeRunner.hasNextLine()) {
      
                          // Go to the next line:
                          mazeRunner.nextLine();
      
                      }
      
                  }
      
                  mazeRunner.close();
      
              } catch (FileNotFoundException ex) {
                  System.out.println(&quot;Unable to open file: &quot; + fileName);
              } catch (IOException ex) {
                  System.out.println(&quot;Error reading file: &quot; + fileName);
              }
      
              return results;
          }
      }
  
  </code></pre>
  <p>I used public static readMaze from here - <code>https://stackoverflow.com/questions/41632526/reading-a-text-file-into-a-2d-array</code></p>
  <p>Print to the console:</p>
  <pre><code>Add size matrix m*n 
  number of row m =
  4
  number of columns n =
  2
  Choose to method assignments matrix 
  0-manually 
  1-automatically
  1
  1 0 
  9 3 
  6 3 
  0 2 
  Quantity of Null in matrix: 2
  
  Matrix record in file
  1   0   
  9   3   
  6   3   
  0   2   
  
  [[I@5e9f23b4, [I@4783da3f, [I@378fd1ac, [I@49097b5d]
  
  Process finished with exit code 0
  </code></pre>
  <p>But this code incorrect reading and printing 2D matrix(array)
  <code>[[I@5e9f23b4, [I@4783da3f, [I@378fd1ac, [I@49097b5d]</code></p>
  <p>I doubt about this code:</p>
  <p><code>ArrayList&lt;int[]&gt; list = readMaze(&quot;D:\\labs1.txt&quot;); System.out.println(Arrays.toString(list.toArray()));</code></p>
  <p>Please Help me, this's need me to finish task in the University</p>