-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTools.java
109 lines (92 loc) · 2.58 KB
/
FileTools.java
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
107
108
109
package tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
* read and write files line by line
* @author Antoine
*
*/
public class FileTools {
public FileTools() {}
/**
* write file line by line with records list
* @param fileName
* @param append true = si le fichier existe, les donn�es sont ajout�es � la suite, false = le fichier est �cras�
* @param records list de string a �crire
* @throws IOException
*/
public static void write(String fileName, boolean append, List<String> records) throws IOException {
boolean first = true;
try {
FileWriter writer = new FileWriter(fileName, append);
// on écrit dans le fichier
for (String string : records) {
if (!first) {
writer.write(System.lineSeparator());
} else {
first = false;
}
writer.write(string);
}
writer.close();
} finally {
}
}
/**
* read file line by line and return arrayList of string
* @param fileName
* @return an arrayList of String
* @throws IOException
*/
public static ArrayList<String> read(String fileName) throws IOException {
ArrayList<String> list = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
for(String line; (line = br.readLine()) != null; ) {
list.add(line);
}
}
return list;
}
public static Set<String> readToSet(String fileName) throws IOException {
Set<String> list = new TreeSet<>();
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
for(String line; (line = br.readLine()) != null; ) {
list.add(line);
}
}
return list;
}
/**
* write file line by line with records list
* @param fileName
* @param append true = si le fichier existe, les donn�es sont ajout�es � la suite, false = le fichier est �cras�
* @param records set de string a �crire
* @throws IOException
*/
public static void write(String fileName, boolean append, Set<String> records) throws IOException {
try {
FileWriter writer = new FileWriter(fileName, append);
// on écrit dans le fichier
for (String string : records) {
writer.write(string);
writer.write(System.lineSeparator());
}
writer.close();
} finally {
}
}
public static boolean fileExist(String filePath) {
File f = new File(filePath);
if(f.exists() && !f.isDirectory()) {
return true;
}
return false;
}
}