-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveAnArraytoFile
More file actions
41 lines (32 loc) · 912 Bytes
/
SaveAnArraytoFile
File metadata and controls
41 lines (32 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.gmail.orfejka;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Main {
/*
* Напишите метод для сохранения в текстовый файл двухмерного массива целых
* чисел.
*/
public static void main(String[] args) {
int array [][] = {{1,2,3},{4,5,5,6,6},{1,2,3,4},{2,3,4,4}};
saveAnArrayToFile(array);
}
static void saveAnArrayToFile(int arr[][]) {
StringBuffer bf = new StringBuffer();
int number;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
number = arr[i][j];
bf.append(number);
bf.append(" ");
}
bf.append(System.lineSeparator());
}
File file = new File("array.txt");
try (PrintWriter pw = new PrintWriter(file)) {
pw.println(bf.toString());
} catch (IOException e) {
System.out.println(e);
}
}
}