Easy Reader and Easy Writer
EasyReader Example
/*******************************************************************************************************
Notes:
You need to
extend java.io.*; for both of these classes.
EasyReader provides simple methods for reading the console
and for opening and reading
text files. Similar for EasyWriter
All exceptions are handled inside the class and are hidden
from the user.
An example is provided below, followed by the EasyReader class
*********************************************************************************************************
import java.io.*;
EasyReader console = new EasyReader();
System.out.print("Enter input file name: ");
String fileName = console.readLine();
EasyReader inFile = new EasyReader(fileName);
if (inFile.bad())
{
System.err.println("Can't open " +
fileName);
System.exit(1);
}
String firstLine = inFile.readLine();
if (!inFile.eof()) // or: if (firstLine != null)
System.out.println("The first line is :
" + firstLine);
System.out.print("Enter the maximum
number of integers to read: ");
int maxCount = console.readInt();
int k, count = 0;
while (count < maxCount && !inFile.eof())
{
k = inFile.readInt();
if (!inFile.eof())
{
// process or store this number
count++;
}
}
inFile.close(); // optional
System.out.println(count + " numbers read");
EasyReader Class
public class EasyReader
{
protected String myFileName;
protected BufferedReader myInFile;
protected int myErrorFlags = 0;
protected static final int OPENERROR = 0x0001;
protected static final int CLOSEERROR = 0x0002;
protected static final int READERROR = 0x0004;
protected static final int EOF = 0x0100;
// Constructor. Prepares console (System.in) for reading
public EasyReader()
{
myFileName = null;
myErrorFlags = 0;
myInFile = new BufferedReader(new
InputStreamReader(System.in), 128);
}
// Constructor. Opens a file for reading. fileName=the
name or pathname of the file.
public EasyReader(String fileName)
{
myFileName = fileName;
myErrorFlags = 0;
try
{
myInFile = new BufferedReader(new
FileReader(fileName), 1024);
}
catch (FileNotFoundException e)
{
myErrorFlags |= OPENERROR;
myFileName = null;
}
}
//Closes the file
public void close()
{
if (myFileName == null)
return;
try
{
myInFile.close();
}
catch (IOException e)
{
System.err.println("Error closing " + myFileName +
"\n");
myErrorFlags |= CLOSEERROR;
}
}
//Checks the status of the file: return true if en error
occurred opening or reading the file
//False otherwise
public boolean bad()
{
return myErrorFlags != 0;
}
//Checks the EOF status of the file
//Returns true if EOF was encountered in the previous read
operation
//Returns false otherwise
public boolean eof()
{
return (myErrorFlags & EOF) != 0;
}
private boolean ready() throws IOException
{
return myFileName == null || myInFile.ready();
}
/************************************************************************
Reads the next character from a file (any character
including a space or a newline
character).
Returns character read
Returns
<code>null</code> character (unicode 0) if trying to read beyond the EOF
********************************************************************************************************/
public char readChar()
{
char ch = '\u0000';
try
{
if (ready())
{
ch = (char)myInFile.read();
}
}
catch (IOException e)
{
if (myFileName != null)
System.err.println("Error reading " + myFileName +
"\n");
myErrorFlags |= READERROR;
}
if (ch == '\u0000')
myErrorFlags |= EOF;
return ch;
}
/**************************************************************************
Reads from the current position in the file up to and
including the next newline character.
The newline character is thrown away.
Returns the read
string (excluding the newline character)
Returns
null if trying to read beyond the EOF
****************************************************************************************************/
public String readLine()
{
String s = null;
try
{
s = myInFile.readLine();
}
catch (IOException e)
{
if (myFileName != null)
System.err.println("Error reading " + myFileName +
"\n");
myErrorFlags |= READERROR;
}
if (s == null)
myErrorFlags |= EOF;
return s;
}
/**********************************************************************
Skips whitespace and reads the next word (a string of
consecutive non-whitespace
characters (up to but excluding the next space, newline,
etc.)
Returns the read string
Returns null if trying to read beyond the EOF.
***********************************************************************************************/
public String readWord()
{
StringBuffer buffer = new StringBuffer(128);
char ch = ' ';
int count = 0;
String s = null;
try
{
while (ready() && Character.isWhitespace(ch))
ch = (char)myInFile.read();
while (ready() && !Character.isWhitespace(ch))
{
count++;
buffer.append(ch);
myInFile.mark(1);
ch = (char)myInFile.read();
};
if (count > 0)
{
myInFile.reset();
s = buffer.toString();
}
else
{
myErrorFlags |= EOF;
}
}
catch (IOException e)
{
if (myFileName != null)
System.err.println("Error reading " + myFileName +
"\n");
myErrorFlags |= READERROR;
}
return s;
}
/********************************************************************
Reads the next integer (without validating its format)
Returns the integer read
Returns
0 if trying to read beyond the EOF
*****************************************************************************************************************/
public int readInt()
{
String s = readWord();
if (s != null)
return Integer.parseInt(s);
else
return 0;
}
/*******************************************************************
Reads the next double (without validating its format)
Returns the number read
Returns
0 if trying to read beyond the EOF
***************************************************************************************************************/
public double readDouble()
{
String s = readWord();
if (s != null)
return Double.parseDouble(s);
else
return 0.;
}
}
Easy Writer Example
import java.io.*;
/**********************************************************************
EasyWriter provides simple methods for opening and writing
to text files.
All exceptions are handled inside the class and are hidden from the user.
****************************************************************************************************************/
EasyWriter outFile = new EasyWriter("anyname.txt");
if (outFile.bad())
{
System.err.println("Can't create anyname.txt\n");
System.exit(1);
}
outFile.print("2 + 2 = ");
outFile.println(4);
outFile.println(); // an extra blank line
outFile.close(); // optional
*********************************************************************/
EasyWriter Class
public class EasyWriter
{
protected String myFileName;
protected PrintWriter myOutFile;
protected int myErrorFlags = 0;
protected static final int OPENERROR = 0x0001;
protected static final int CLOSEERROR = 0x0002;
protected static final int WRITEERROR = 0x0004;
/*********************************************************************
Constructor. Creates a new file (or truncates an
existing file)
fileName=the name of the file to be created
**********************************************************************************************************/
public EasyWriter(String fileName)
{
this(fileName, null);
}
/*********************************************************************
Constructor.
Creates a new file. If the file exists can append to it.
fileName=the name of the file to be created.
If equals to "app" opens in append mode
*********************************************************************************************************/
public EasyWriter(String fileName, String mode)
{
myFileName = fileName;
myErrorFlags = 0;
try
{
myOutFile = new PrintWriter(new FileWriter(fileName,
"app".equals(mode)));
}
catch (IOException e)
{
myErrorFlags |= OPENERROR;
myFileName = null;
}
}
//Closes the file
public void close()
{
if (myFileName != null)
myOutFile.close();
}
/********************************************************************
Checks the status of the file
Returns true if an error occurred opening or writing to the
file
Returns false otherwise
*****************************************************************************************************************/
public boolean bad()
{
return myErrorFlags != 0;
}
// Writes one character to the file ch character to be
written
public void print(char ch)
{
myOutFile.print(ch);
}
// Writes an integer to the file k number to be written
public void print(int k)
{
myOutFile.print(k);
}
//Writes a double to the file, xnumber to be written
public void print(double x)
{
myOutFile.print(x);
}
//Writes a string to the file s string to be written
public void print(String s)
{
myOutFile.print(s);
}
// Writes a newline character to the file
public void println()
{
myOutFile.println();
}
// Writes one character and newline to the file ch
character to be written
public void println(char ch)
{
myOutFile.println(ch);
}
//Writes an integer and newline to the file, k number to be written
public void println(int k)
{
myOutFile.println(k);
}
// Writes a double and newline to the file x number to be
written
public void println(double x)
{
myOutFile.println(x);
}
// Writes a string and newline to the file s string to be
written
public void println(String s)
{
myOutFile.println(s);
}
}