forked from yegor256/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
58 lines (51 loc) · 1.33 KB
/
Parser.java
File metadata and controls
58 lines (51 loc) · 1.33 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
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This class is thread safe.
*/
public class Parser {
private ThreadLocal<File> file = new ThreadLocal<>();
public synchronized void setFile(File f) {
file.set(f);
}
public synchronized File getFile() {
return file.get();
}
public String getContent() throws IOException {
StringBuilder output = new StringBuilder("");
File file = getFile();
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file))) {
int data;
while ((data = bis.read()) > -1) {
output.append((char) data);
}
}
return output.toString();
}
public String getContentWithoutUnicode() throws IOException {
StringBuilder output = new StringBuilder("");
File file = getFile();
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file))) {
int data;
while ((data = bis.read()) > -1) {
if (data < 0x80) {
output.append((char) data);
}
}
}
return output.toString();
}
public void saveContent(String content) throws IOException {
File file = getFile();
try (FileOutputStream o = new FileOutputStream(file)) {
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
}
}
}