forked from yegor256/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
71 lines (60 loc) · 2.07 KB
/
Parser.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
import com.sun.org.apache.regexp.internal.recompile;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
/**
* This class is thread safe.
*/
public class Parser {
private Path filePath;
public synchronized void setFile(File file) {
filePath = file.toPath();
}
public synchronized File getFile() {
return filePath.toFile();
}
public synchronized Path getPath() {
return this.filePath;
}
public synchronized void setPath(Path path) {
this.filePath = path;
}
public Parser(Path path) {
this.filePath = path;
}
/**
* @return
* @throws IOException
* @throws java.lang.NullPointerException if file = null
* @throws java.io.FileNotFoundException if file not exists
*/
public synchronized String getContent() throws IOException {
Charset charset = Charset.defaultCharset();
Scanner scanner = new Scanner(filePath, charset.name());
scanner.next();
StringBuilder destinationStringBuilder = new StringBuilder();
String read = null;
while ((read = scanner.next()) != null) {
destinationStringBuilder.append(read);
}
return destinationStringBuilder.toString();
}
public synchronized String getContentWithoutUnicode() throws IOException {
BufferedReader bufferedReader = Files.newBufferedReader(filePath, Charset.defaultCharset());
StringBuilder destinationStringBuilder = new StringBuilder();
int data;
while ((data = bufferedReader.read()) > 0 && (data < 0x80)) {
destinationStringBuilder.append((char) data);
}
return destinationStringBuilder.toString();
}
public synchronized void saveContent(String content) throws IOException {
BufferedWriter bufferedWriter = Files.newBufferedWriter(filePath, Charset.defaultCharset());
bufferedWriter.write(content);
bufferedWriter.flush();
}
}