forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (52 loc) · 2.08 KB
/
Main.java
File metadata and controls
66 lines (52 loc) · 2.08 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package modern.challenge;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Random;
public class Main {
public static void main(String[] args) throws IOException {
Random rnd = new Random();
int[] intValues = new int[10];
double[] doubleValues = new double[10];
Arrays.setAll(intValues, (t) -> rnd.nextInt(100_000));
Arrays.setAll(doubleValues, (t) -> rnd.nextDouble());
Path path1 = Paths.get("noformatter.txt");
try (BufferedWriter bw = Files.newBufferedWriter(
path1, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
for (int i = 0; i < 10; i++) {
bw.write("| " + intValues[i] + " | " + doubleValues[i] + " | ");
bw.newLine();
}
}
Path path2 = Paths.get("withformatter1.txt");
try (BufferedWriter bw = Files.newBufferedWriter(
path2, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
for (int i = 0; i < 10; i++) {
bw.write(String.format("| %6s | %.3f |", intValues[i], doubleValues[i]));
bw.newLine();
}
}
Path path3 = Paths.get("withformatter2.txt");
try (Formatter output = new Formatter(path3.toFile())) {
for (int i = 0; i < 10; i++) {
output.format("| %6s | %.3f |%n", intValues[i], doubleValues[i]);
}
}
Path path4 = Paths.get("withformatter3.txt");
DecimalFormat formatter = new DecimalFormat("###,### bytes");
try (Formatter output = new Formatter(path4.toFile())) {
for (int i = 0; i < 10; i++) {
output.format("%12s%n", formatter.format(intValues[i]));
}
}
}
}