Skip to content

Commit b9bd671

Browse files
committed
Structure of file updated
1 parent 5c0cb69 commit b9bd671

File tree

1 file changed

+66
-37
lines changed

1 file changed

+66
-37
lines changed

Parser.java

+66-37
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,71 @@
1-
import java.io.File;
2-
import java.io.FileInputStream;
3-
import java.io.FileOutputStream;
4-
import java.io.IOException;
1+
import com.sun.org.apache.regexp.internal.recompile;
2+
3+
import java.io.*;
4+
import java.nio.ByteBuffer;
5+
import java.nio.CharBuffer;
6+
import java.nio.charset.Charset;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Scanner;
10+
511
/**
612
* This class is thread safe.
713
*/
814
public class Parser {
9-
private File file;
10-
public synchronized void setFile(File f) {
11-
file = f;
12-
}
13-
public synchronized File getFile() {
14-
return file;
15-
}
16-
public String getContent() throws IOException {
17-
FileInputStream i = new FileInputStream(file);
18-
String output = "";
19-
int data;
20-
while ((data = i.read()) > 0) {
21-
output += (char) data;
22-
}
23-
return output;
24-
}
25-
public String getContentWithoutUnicode() throws IOException {
26-
FileInputStream i = new FileInputStream(file);
27-
String output = "";
28-
int data;
29-
while ((data = i.read()) > 0) {
30-
if (data < 0x80) {
31-
output += (char) data;
32-
}
33-
}
34-
return output;
35-
}
36-
public void saveContent(String content) throws IOException {
37-
FileOutputStream o = new FileOutputStream(file);
38-
for (int i = 0; i < content.length(); i += 1) {
39-
o.write(content.charAt(i));
40-
}
41-
}
15+
private Path filePath;
16+
17+
public synchronized void setFile(File file) {
18+
filePath = file.toPath();
19+
}
20+
21+
public synchronized File getFile() {
22+
return filePath.toFile();
23+
}
24+
25+
public synchronized Path getPath() {
26+
return this.filePath;
27+
}
28+
29+
public synchronized void setPath(Path path) {
30+
this.filePath = path;
31+
}
32+
33+
34+
public Parser(Path path) {
35+
this.filePath = path;
36+
}
37+
38+
/**
39+
* @return
40+
* @throws IOException
41+
* @throws java.lang.NullPointerException if file = null
42+
* @throws java.io.FileNotFoundException if file not exists
43+
*/
44+
public synchronized String getContent() throws IOException {
45+
Charset charset = Charset.defaultCharset();
46+
Scanner scanner = new Scanner(filePath, charset.name());
47+
scanner.next();
48+
StringBuilder destinationStringBuilder = new StringBuilder();
49+
String read = null;
50+
while ((read = scanner.next()) != null) {
51+
destinationStringBuilder.append(read);
52+
}
53+
return destinationStringBuilder.toString();
54+
}
55+
56+
public synchronized String getContentWithoutUnicode() throws IOException {
57+
BufferedReader bufferedReader = Files.newBufferedReader(filePath, Charset.defaultCharset());
58+
StringBuilder destinationStringBuilder = new StringBuilder();
59+
int data;
60+
while ((data = bufferedReader.read()) > 0 && (data < 0x80)) {
61+
destinationStringBuilder.append((char) data);
62+
}
63+
return destinationStringBuilder.toString();
64+
}
65+
66+
public synchronized void saveContent(String content) throws IOException {
67+
BufferedWriter bufferedWriter = Files.newBufferedWriter(filePath, Charset.defaultCharset());
68+
bufferedWriter.write(content);
69+
bufferedWriter.flush();
70+
}
4271
}

0 commit comments

Comments
 (0)