package lab6; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Random; import java.util.Scanner; public class Zad4 { static Random rand = new Random(); static Scanner scanner = null; static PrintWriter writer = null; static int[][] arr = new int[10][10]; static int[][] arr2 = new int[10][10]; static void initialize() { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { arr[i][j] = rand.nextInt(99); } } } static void show(int[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { System.out.printf("%2d ", a[i][j]); } System.out.printf("\n"); } System.out.printf("\n"); } static void write() { try { writer = new PrintWriter("data.csv", "UTF-8"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { writer.printf("%d ", arr[i][j]); } writer.printf("\n"); } } catch (FileNotFoundException e) { System.out.printf("File not found!\n"); } catch (UnsupportedEncodingException e) { System.out.printf("Unsupported encoding!\n"); } finally { if (writer != null) { writer.close(); } else { System.out.println("PrintWriter not open"); } } } static void open() { Path filePath = Paths.get("data.csv"); try { scanner = new Scanner(filePath); int a = 0; for (int i = 0; i < arr2.length; i++) { for (int j = 0; j < arr2.length; j++) { if (scanner.hasNextInt()) { a = scanner.nextInt(); arr2[i][j] = a; } } } } catch (IOException e) { System.out.println("IOException!!!!!!!!"); } } public static void main(String[] args) { initialize(); show(arr); write(); open(); show(arr2); } }