first commit
This commit is contained in:
214
CS1322/p3/FileChanger.java
Normal file
214
CS1322/p3/FileChanger.java
Normal file
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* FileChanger.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 05, 2002
|
||||
* Created the FileChanger class
|
||||
* 1.1 Oct. 06, 2002
|
||||
* Compiled, Commented
|
||||
* 2.0 Oct. 06, 2002
|
||||
* Deleted all, start from scratch, clean up code
|
||||
* 2.1 Oct. 06, 2002
|
||||
* Completed classes
|
||||
* 2.2 Oct. 06, 2002
|
||||
* Commented
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 2.2, Oct. 06, 2002
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class FileChanger {
|
||||
|
||||
//variables
|
||||
private String strFileName;
|
||||
|
||||
//readers and writers
|
||||
FileInputStream fis;
|
||||
ObjectInputStream ois;
|
||||
|
||||
FileOutputStream fos;
|
||||
ObjectOutputStream oos;
|
||||
|
||||
public static final boolean bDebug = false; //there's nothing to debug
|
||||
//that bDebug can handle
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for FileChanger
|
||||
*@param strFileName the name to be given to the output file
|
||||
*/
|
||||
public FileChanger(String strFileName){
|
||||
this.strFileName = strFileName;
|
||||
}
|
||||
|
||||
////////////////////////////
|
||||
//Initializers/Destructors//
|
||||
////////////////////////////
|
||||
|
||||
/**
|
||||
*Initialize the reader streams
|
||||
*@return true if successful, false if file not found
|
||||
*/
|
||||
public boolean initReader(){
|
||||
|
||||
//init file input stream
|
||||
try{
|
||||
fis = new FileInputStream(strFileName);
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
return false;
|
||||
}
|
||||
|
||||
//init obj input stream
|
||||
try{
|
||||
ois = new ObjectInputStream(fis);
|
||||
}
|
||||
catch(IOException ioe){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Initialize the write streams
|
||||
*/
|
||||
public void initWriter(){
|
||||
|
||||
//init file output stream
|
||||
try{
|
||||
fos = new FileOutputStream(strFileName);
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
System.err.println("File not Found! - initWriter()");
|
||||
}
|
||||
|
||||
//init obj output stream
|
||||
try{
|
||||
oos = new ObjectOutputStream(fos);
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - initWriter()");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Close read streams
|
||||
*/
|
||||
public void closeRead(){
|
||||
|
||||
try{
|
||||
fis.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - closeRead()");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Close write streams
|
||||
*/
|
||||
public void closeWrite(){
|
||||
|
||||
try{
|
||||
oos.flush();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - flush objstream - closeWrite()");
|
||||
}
|
||||
|
||||
try{
|
||||
fos.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - close filestream - closeWrite()");
|
||||
}
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Retrieve the next object in the file
|
||||
*/
|
||||
public Object nextObject(){
|
||||
try{
|
||||
return ois.readObject();
|
||||
}
|
||||
catch(EOFException eof){
|
||||
return null;
|
||||
}
|
||||
catch(ClassNotFoundException cnfe){
|
||||
System.err.println("Class not found - nextObject()");
|
||||
return null;
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IO Exception - nextObject()");
|
||||
return null;
|
||||
}
|
||||
catch(NullPointerException npe){
|
||||
System.err.println("Null Pointer Exception - nextObject()");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*Write an Object to the file
|
||||
*@param toWrite, the object to be written
|
||||
*/
|
||||
public void writeObject(Object toWrite){
|
||||
try{
|
||||
oos.writeObject(toWrite);
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IO Exception - writeObject()");
|
||||
}
|
||||
}
|
||||
|
||||
/**********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class FileChanger.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
FileChanger fg = new FileChanger("This stinks.txt");
|
||||
|
||||
fg.initReader();
|
||||
fg.initWriter();
|
||||
|
||||
fg.writeObject(new Integer(7));
|
||||
fg.writeObject(new Integer(6));
|
||||
fg.writeObject(new String("Bob is a person too"));
|
||||
fg.writeObject(new Boolean(false));
|
||||
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
|
||||
fg.closeRead();
|
||||
fg.closeWrite();
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class FileChanger
|
||||
Reference in New Issue
Block a user