-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIOUtils.java
More file actions
106 lines (95 loc) · 3.01 KB
/
Copy pathFileIOUtils.java
File metadata and controls
106 lines (95 loc) · 3.01 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.java8;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
/**
* @author 212331901
* @date 2019/3/11
*/
public class FileIOUtils {
private final Charset charset = Charset.forName("UTF-8");
/**
* Files.newBufferedReader
* @param path
*/
public void readAndPrintFile(String path) {
Path path1 = Paths.get(path);
try(BufferedReader bufferedReader = Files.newBufferedReader(path1, charset)){
String line = null;
while ((line = bufferedReader.readLine()) != null) {
Arrays.stream(line.split(" ")).forEach(System.out::println);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Files.lines -> stream
* @param path
*/
public void readPrintFileLines(String path) {
Path path1 = Paths.get(path);
try{
Files.lines(path1).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Files.write string
* @param dest
*/
public void writeStringToFile(String dest) {
Path path1 = Paths.get(dest);
Charset charset = Charset.forName("UTF-8");
String line = "CNN JW 2019-4-1 2019-5-1 20\n";
try {
Files.write(path1, line.getBytes(charset));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Files.write iterable
* @param dest
*/
public void appendStringsToFile(String dest) {
Path path1 = Paths.get(dest);
Charset charset = Charset.forName("UTF-8");
String line = "CNN JW 2019-4-1 2019-5-1 20";
String line2 = "ABC JW 2019-4-1 2019-5-1 30";
try {
Files.write(path1, Arrays.asList(line, line2), charset, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Files.newBufferedWriter
* @param dest
*/
public void writeStringToFileBuffered(String dest) {
Path path1 = Paths.get(dest);
Charset charset = Charset.forName("UTF-8");
String line = "CNN JW 2019-4-1 2019-5-1 10\n";
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path1, charset)) {
bufferedWriter.write(line);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileIOUtils fileIOUtils = new FileIOUtils();
// fileIOUtils.readAndPrintFile("C:\\git_repo\\booking\\src\\main\\resources\\orderOfCity.txt");
String dest = "C:\\git_repo\\booking\\src\\main\\resources\\toOrderOfCity.txt";
// fileIOUtils.writeStringToFile(dest);
fileIOUtils.appendStringsToFile(dest);
fileIOUtils.readPrintFileLines(dest);
}
}