-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNIOUtils.java
More file actions
51 lines (39 loc) · 1.58 KB
/
Copy pathNIOUtils.java
File metadata and controls
51 lines (39 loc) · 1.58 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
package io_nio2.nio;
import io_nio2.Constant;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
public class NIOUtils {
public static void main(String[] args) throws IOException {
writeFileWithFiles(Constant.COMMON_OUTPUT_PATH, "sadadadasdasd");
String read = readFileWithFiles(Constant.COMMON_INPUT_PATH);
System.out.println(read);
boolean fileWithFiles = createFileWithFiles("src/resource/file/Output23231.txt");
System.out.println("Is Created File: "+fileWithFiles);
boolean isDeletedFile = deleteFileWithFiles("src/resource/file/Output23231.txt");
System.out.println("Is Deleted File: "+isDeletedFile);
}
public static String readFileWithFiles(String filePath) throws IOException {
Path path = Path.of(filePath);
return String.join("\n", Files.readAllLines(path));
}
public static void writeFileWithFiles(String filePath, String content) throws IOException {
Path path = Path.of(filePath);
Files.write(path, content.getBytes());
}
public static boolean createFileWithFiles(String filePath) throws IOException {
Path path = Path.of(filePath);
if (!Files.exists(path)) {
Files.createFile(path);
}
return Files.exists(path);
}
public static boolean deleteFileWithFiles(String filePath) throws IOException {
Path path = Path.of(filePath);
if (Files.exists(path)) {
Files.delete(path);
}
return !Files.exists(path);
}
}