package com.Bit4Tat;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
this.filename = filename;
}
try {
if (filename != null) {
out = new BufferedWriter(new FileWriter(filename));
writer = true;
}
} catch (IOException e) {
System.err.println("There was a problem opening the requested file " + filename + ".");
System.err.println("Error: " + e);
System.exit(1);
}
}
try {
if (filename != null) {
in = new BufferedReader(new FileReader(filename));
reader = true;
}
} catch (IOException e) {
System.err.println("There was a problem opening the requested file " + filename + ".");
System.err.println("Error: " + e);
System.exit(1);
}
}
public void writeToken (String token, String delimiter) {
try {
if (writer) {
out.write(token);
if (delimiter != null)
out.write(delimiter);
} else {
throw (new Exception("The writer has not yet been initialized for writing."));
}
} catch (IOException e) {
System.err.println("There was a problem writing to the requested file " + out.toString() + ".");
System.err.println("Error: " + e);
System.exit(1);
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
public void writeLine (String line,
boolean newline) {
try {
if (writer) {
out.write(line);
if (newline)
writeNewLine();
} else {
throw (new Exception("The writer has not yet been initialized for writing."));
}
} catch (IOException e) {
System.err.println("There was a problem writing to the requested file " + out.toString() + ".");
System.err.println("Error: " + e);
System.exit(1);
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
try {
if (writer) {
out.write('\n');
} else {
throw (new Exception("The writer has not yet been initialized for writing."));
}
} catch (IOException e) {
System.err.println("There was a problem writing to the requested file " + out.toString() + ".");
System.err.println("Error: " + e);
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
String temp = "";
String fileContents = "";
StringTokenizer st = null;
try {
if (reader) {
while ((temp = in.readLine()) != null) {
fileContents = fileContents + temp + " ";
}
if (delimiter == null)
st = new StringTokenizer(fileContents);
else
st = new StringTokenizer(fileContents, delimiter);
} else {
throw (new Exception("The reader has not yet been initialized for reading."));
}
} catch (IOException e) {
System.err.println("There was a problem processing a string token.");
System.err.println("Error: " + e);
System.exit(1);
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return st;
}
try {
if (writer)
out.close();
} catch (IOException e) {
System.err.println("There was a problem closing the requested file.");
System.err.println("Error: " + e);
System.exit(1);
}
}
try {
if (reader)
in.close();
} catch (IOException e) {
System.err.println("There was a problem closing the requested file.");
System.err.println("Error: " + e);
System.exit(1);
}
}
BufferedWriter out;
BufferedReader in;
boolean writer;
boolean reader;
String filename;
}