Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JSONWriter extends Writer {
- private JSONObject json = null;
- private boolean closed = true;
- public JSONWriter () {
- closed = false;
- }
- public JSONWriter (String text) throws JSONFormatException {
- json = new JSONObject(text);
- closed = false;
- }
- public JSONWriter (File file) throws JSONFormatException {
- String text = "";
- for (String s : Files.readAllLines(file.toPath())) text = text.concat(s);
- json = new JSONObject(text);
- closed = false;
- }
- public JSONWriter (File file, boolean overwrite) throws JSONFormatException {
- if (!overwrite) {
- String text = "";
- for (String s : Files.readAllLines(file.toPath())) text = text.concat(s);
- }
- json = new JSONObject(text);
- closed = false;
- }
- public void flush () {
- json = null;
- }
- public void close () {
- this.flush();
- closed = true;
- }
- public void write (char[] buffer) {
- this.write(new String(buffer));
- }
- public void write (char[] buffer, int offset, int length) {
- if (offset + length > buffer.length) throw new IllegalArgumentException("Offset and length combined cannot be greater than buffer length.");
- char[] intermediate = new char[length];
- System.arraycopy(buffer, offset, intermediate, 0, length);
- this.write(new String(intermediate));
- }
- public void write (int i) {
- this.write(new char[1]{(char) i});
- }
- public void write (String buffer) {
- this.write(buffer, 0, buffer.length());
- }
- public void write (String buffer, int offset, int length) {
- if (json == null) json = new JSONObject();
- if (offset + length > buffer.length()) throw new IllegalArgumentException("Offset and length combined cannot be greater than buffer length.");
- json.add(buffer.substring(offset, offset + length));
- }
- public void addEntry (String key, Object value) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement