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
70 lines (61 loc) · 1.75 KB
/
Copy pathParser.java
File metadata and controls
70 lines (61 loc) · 1.75 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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This class is thread safe.
*/
public interface Parser {
public String content() throws IOException;
}
public interface Writer {
public void save(String content) throws IOException;
}
public final class SimpleFileParser implements Parser {
private final File file;
public SimpleFileParser(File file) {
this.file = file;
}
public String content() throws IOException {
try (FileInputStream inputStream = new FileInputStream(this.file)) {
StringBuilder output = new StringBuilder();
int data;
while ((data = inputStream.read()) > 0) {
output.append((char) data);
}
return output.toString();
}
}
}
public final class FileParserWithoutUnicode implements Parser {
private final SimpleFileParser simpleFileParser;
private static final int UNICODE_CHAR = 0x80;
public FileParserWithoutUnicode(SimpleFileParser simpleFileParser) {
this.simpleFileParser = simpleFileParser;
}
public String content() throws IOException {
StringBuilder output = new StringBuilder();
String fileContent = simpleFileParser.content();
for(int i = 0; i < fileContent.length(); i++){
char symbol = fileContent.charAt(i);
if((int)symbol < UNICODE_CHAR){
output.append(symbol);
}
}
return output.toString();
}
}
public final class FileWriter implements Writer{
private final File file;
public FileWriter(File file){
this.file = file;
}
@Override
public void save(String content) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(this.file)) {
for (int i = 0; i < content.length(); i++) {
fileOutputStream.write(content.charAt(i));
}
}
}
}