Created
August 7, 2024 13:41
-
-
Save denis-petrov/bd35a8efea479bfe71b8a8c838128554 to your computer and use it in GitHub Desktop.
quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 File file; | |
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; | |
} | |
public String getContentWithoutUnicode() throws IOException { | |
FileInputStream i = new FileInputStream(file); | |
String output = ""; | |
int data; | |
while ((data = i.read()) > 0) { | |
if (data < 0x80) { | |
output += (char) data; | |
} | |
} | |
return output; | |
} | |
public void saveContent(String content) { | |
FileOutputStream o = new FileOutputStream(file); | |
try { | |
for (int i = 0; i < content.length(); i += 1) { | |
o.write(content.charAt(i)); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment