-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewFileMethods.java
More file actions
63 lines (54 loc) · 2.15 KB
/
NewFileMethods.java
File metadata and controls
63 lines (54 loc) · 2.15 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
package com.ibrahimatay;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class NewFileMethods {
public static void main(String[] args) {
// java.io.File
// https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html
// java.nio.file.Files.writeString writes garbled UTF-16 instead of UTF-8
// https://bugs.openjdk.org/browse/JDK-8209576
// Add methods to Files for reading/writing a string from/to a file
// https://bugs.openjdk.org/browse/JDK-8202055
// Reading and writing file in ISO-8859-1 encoding?
// https://stackoverflow.com/questions/63363359/reading-and-writing-file-in-iso-8859-1-encoding
readString();
readWriteString();
readWriteStringWithCharset();
}
private static void readString() {
try {
Path path = Paths.get("test01.txt");
String data = Files.readString(path);
System.out.println(data);
System.out.println(data.getClass().getName());
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
private static void readWriteString() {
try {
Path path = Paths.get("test02.txt");
path = Files.writeString(path, "Welcome to Istanbul!!");
String data = Files.readString(path);
System.out.println(data);
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
private static void readWriteStringWithCharset() {
try {
Path path = Paths.get("test03.txt");
// java.nio.charset.StandardCharsets
// https://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html
path = Files.writeString(path, "Welcome to Istanbul!!", Charset.forName(StandardCharsets.UTF_8.name()));
String data = Files.readString(path);
System.out.println(data);
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
}