Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*~
24 changes: 13 additions & 11 deletions Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;

/**
* This class is thread safe.
*/
public class Parser {
private File file;
//I would remove the setter/getter for file and make it a parameter to the constructor.
//If a user needs to change the file, it should require a new parser.
public synchronized void setFile(File f) {
file = f;
}
public synchronized File getFile() {
return file;
}

public String getContent() throws IOException {
FileInputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
output += (char) data;
}
return output;
byte[] bytes = Files.readAllBytes(file.toPath());
return new String(bytes);
}
//TODO: implement a FilteredInputStream that takes a lambda for which characters to skip
//this would allow alternate cases to be easily implemented and use that stream to read
//the character. Also would use a stringbuffer instead of string concat (more performant)
public String getContentWithoutUnicode() throws IOException {
FileInputStream i = new FileInputStream(file);
String output = "";
Expand All @@ -34,9 +38,7 @@ public String getContentWithoutUnicode() throws IOException {
return output;
}
public void saveContent(String content) throws IOException {
FileOutputStream o = new FileOutputStream(file);
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
PrintWriter writer = new PrintWriter(file);
writer.print(content);
}
}