Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import nice.core.fxn.Sieve; // See http://pastebin.com/Evp36u52
- /**
- * Version 1.4
- * Reads from and writes to files. Necessary when Scanner goes awry in file io is needed.
- * <ul>
- * <li>1.4 - Now supports writing to files as well! And encodings! Switching between reading and writing is also handled (but involves a lot of file operations and is not advised safe when debugging.)
- * <li>1.3 - nemaed from FReader to NiceFile, and made its own library
- * </ul>
- * @author Katie
- */
- public final class NiceFile {
- public static Sieve<String> commentedLineIgnorer(final String commentedLinesStartWith) {
- return new Sieve<String>() {
- @Override
- public Boolean eval(String in) {
- return in.startsWith(commentedLinesStartWith);
- }
- };
- }
- public static final char READ = 'r';
- public static final char OVERWRITE = 'w';
- public static final char APPEND = 'a';
- private int rcounter;
- private BufferedReader reader;
- private FileWriter writer;
- private String line;
- private String fname;
- private char method;
- private Sieve<String> lineIgnorer = null;
- /**
- * Read form a file (method=NiceFile.READ), ignoring all lines for which lineignore.eval(line contents) returns true.
- * @param fname
- * @param commentedLineMarker
- */
- public NiceFile(String fname, Sieve<String> lineIgnorer) {
- this(fname, READ);
- this.lineIgnorer = lineIgnorer;
- }
- /**
- * Opens a stream to the specified file. Use isOK() to test success; no file not found exceptions will be thrown here.
- * @param fname
- */
- public NiceFile(String fname, char method) {
- this.fname = fname;
- start(method);
- }
- /**
- * Get the file name
- * @return
- */
- public String getName() {
- return fname;
- }
- private void start(char method) {
- line = null;
- reader = null;
- line = null;
- reader = null;
- rcounter = 0;
- this.method = method;
- if (method == OVERWRITE || method == APPEND) {
- this.method = method;
- try {
- writer = new FileWriter(fname, method == APPEND);
- } catch (IOException ex) {
- }
- } else {
- this.method = READ;
- try {
- reader = new BufferedReader(new FileReader(fname));
- } catch (FileNotFoundException ex) {
- }
- }
- }
- /**
- * Tests whether the stream to the file has been opened successfully.
- * @return true iff the stream is opened and ready to read from
- */
- public boolean isReaderOK() {
- return reader != null;
- }
- /**
- * Tests whether the write stream to the file has been opened successfully.
- * @return true iff the stream is opened and ready to write to
- */
- public boolean isWriterOK() {
- return writer != null;
- }
- /**
- * Reads the next line in the file. Will return null if the stream is, or suddenly becomes, broken.
- * @return the next line
- */
- public String nextLine() {
- if (method != READ) {
- int rc = rcounter;
- this.stop();
- this.start(APPEND);
- rcounter = rc;
- NiceFile temp = new NiceFile(fname,lineIgnorer);
- temp.fastForward(rcounter);
- rcounter++;
- return temp.nextLine();
- }
- if (reader == null) {
- return null;
- }
- try {
- line = reader.readLine();
- rcounter++;
- } catch (IOException ex) {
- line = null;
- reader = null;
- }
- if (lineIgnorer != null && lineIgnorer.eval(line)) {
- return nextLine();
- } else {
- return line;
- }
- }
- /**
- * Returns the latest line fetched by nextLine();
- * @return
- */
- public String currentLine() {
- return line;
- }
- /**
- * Returns the number of lines read (useful for debugging).
- * @return
- */
- public int linesRead() {
- return rcounter;
- }
- /**
- * Severs the stream.
- * @return
- */
- public boolean stop() {
- if (reader == null && method == READ) {
- return false;
- }
- if (writer == null) {
- return false;
- }
- if (method == READ) {
- try {
- reader.close();
- return true;
- } catch (IOException ex) {
- return false;
- }
- }
- try {
- writer.close();
- return true;
- } catch (IOException ex) {
- return false;
- }
- }
- public String get() {
- StringBuilder sb = new StringBuilder();
- while (nextLine() != null) {
- sb.append(currentLine()).append("\n");
- }
- return sb.toString();
- }
- public ArrayList<String> getList(boolean trimmed) {
- ArrayList<String> out = new ArrayList<String>();
- while (nextLine() != null) {
- String l = currentLine();
- if (trimmed) {
- l = l.trim();
- }
- if (trimmed && l.isEmpty()) {
- continue;
- }
- out.add(l);
- }
- return out;
- }
- public boolean writeln(CharSequence cs) {
- if (method != READ) {
- if (writer == null) {
- return false;
- }
- try {
- writer.append(cs);
- writer.append("\n");
- return true;
- } catch (IOException ex) {
- }
- } else {
- int rc = rcounter;
- this.stop();
- NiceFile temp = new NiceFile(fname, APPEND);
- boolean result = temp.writeln(cs);
- temp.stop();
- this.start(READ);
- fastForward(rc);
- return result;
- }
- return false;
- }
- public boolean write(CharSequence cs) {
- if (method != READ) {
- if (writer == null) {
- return false;
- }
- try {
- writer.append(cs);
- return true;
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- } else {
- int rc = rcounter;
- this.stop();
- NiceFile temp = new NiceFile(fname, APPEND);
- boolean result = temp.write(cs);
- temp.stop();
- this.start(READ);
- fastForward(rc);
- return result;
- }
- return false;
- }
- public boolean fastForward(int goal) {
- while (goal > rcounter) {
- if (nextLine() == null) {
- return false;
- }
- }
- return true;
- }
- public static void main(String args[]) {
- NiceFile nf = new NiceFile("test.txt", NiceFile.OVERWRITE);
- System.out.println(nf.writeln("test 1"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- System.out.println(nf.writeln("test 2"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- System.out.println(nf.writeln("test 3"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- nf.stop();
- nf = new NiceFile("test.txt", NiceFile.APPEND);
- System.out.println(nf.writeln("test 1"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- System.out.println(nf.writeln("test 2"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- System.out.println(nf.writeln("test 3"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- nf.stop();
- nf = new NiceFile("test.txt", NiceFile.READ);
- System.out.println(nf.writeln("test 1"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- System.out.println(nf.writeln("test 2"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- System.out.println(nf.writeln("test 3"));
- System.out.println(nf.nextLine() + " @" + nf.rcounter);
- nf.stop();
- }
- public static boolean append(String filename, CharSequence in) {
- NiceFile nf = new NiceFile(filename, APPEND);
- boolean out = nf.write(in);
- nf.stop();
- return out;
- }
- public static boolean write(String filename, CharSequence in) {
- NiceFile nf = new NiceFile(filename, OVERWRITE);
- boolean out = nf.write(in);
- nf.stop();
- return out;
- }
- public static String read(String filename, String... commentDelim) {
- NiceFile nf = new NiceFile(filename,commentDelim.length==0?null:commentedLineIgnorer(commentDelim[0]));
- return nf.get();
- }
- public static ArrayList<String> read(String filename, boolean trimmed, String... commentDelim) {
- NiceFile nf = new NiceFile(filename,commentDelim.length==0?null:commentedLineIgnorer(commentDelim[0]));
- return nf.getList(trimmed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement