forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaWriteFile.java
More file actions
29 lines (22 loc) · 791 Bytes
/
JavaWriteFile.java
File metadata and controls
29 lines (22 loc) · 791 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
package com.zetcode;
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.util.ArrayList;
import java.util.List;
public class JavaWriteFile {
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/resources/myfile.txt");
List<String> lines = new ArrayList<>();
lines.add("blue sky");
lines.add("sweet orange");
lines.add("fast car");
lines.add("old book");
Files.write(myPath, lines, StandardCharsets.UTF_8,
StandardOpenOption.CREATE);
System.out.println("Data written");
}
}