Please send questions to
st10@humboldt.edu .
/**
* class: OutputPlay
*
* @author Sharon Tuttle
* @version 5-6-03
*/
// this IS needed... for BufferedReader, etc.
import java.io.*;
public class OutputPlay
{
/*--------------------------------------------------------
fields
----------------------------------------------------------*/
int value;
String name;
/*--------------------------------------------------------
constructors
-----------------------------------------------------------*/
OutputPlay(int v, String n)
{
this.value = v;
this.name = n;
}
/*----------------------------------------------------------
accessors
----------------------------------------------------------*/
public int getValue()
{
return this.value;
}
public String getName()
{
return this.name;
}
/*--------------------------------------------------------
modifiers
----------------------------------------------------------*/
public void setValue(int newV)
{
this.value = newV;
}
public void setName(String newN)
{
this.name = newN;
}
/*---------------------------------------------------------
overridden methods
----------------------------------------------------------*/
public String toString()
{
return "OutputPlay[value: " + this.value + " name: " +
this.name + "]\n";
}
/*-------------------------------------------------------
other methods
--------------------------------------------------------*/
public void outputData()
{
System.out.print("value: " + this.value + " ");
System.out.println("name: " + this.name);
}
/*-----------------------------------------------------
terminalInput
Purpose: give an example of terminal input, AND change
the name and value of the calling instance to
the values entered
--------------------------------------------------------*/
// NOTE --- this is sometimes a
// way to avoid catching an
// exception --- declare your method
// as throwing the kind of exception
// that may be thrown by something it
// calls?
public void terminalInput() throws IOException
{
// set up standard input to handle reading of
// one line at a time
BufferedReader input = new BufferedReader(
new InputStreamReader (System.in) );
// ask user to enter a value
System.out.print("please enter an integer value: ");
String inputString = input.readLine();
// convert string to corresponding int
int newVal = Integer.parseInt(inputString);
// ask user to enter a name
System.out.print("please enter a name: ");
String newName = input.readLine();
// perhaps change object calling this to have these
// values?
this.value = newVal;
this.name = newName;
}
/*---------------------------------------------------------
readFromFile
Purpose: to demonstrate file input, and also to read in
a new value and a new name for this instance
from a file in the current directory requested
from the user
----------------------------------------------------------*/
public void readFromFile() throws IOException
{
// what IS the current working directory?
String currentDirectory = System.getProperty("user.dir");
System.out.println("currentDirectory: " + currentDirectory);
// set up standard input to handle reading of
// one line at a time
BufferedReader userInput = new BufferedReader(
new InputStreamReader (System.in) );
System.out.println("please enter file name: ");
String fileName = userInput.readLine();
// create a File object instance
File fileToRead = new File(fileName);
// is this currently the name of an existing, readable file
// in the current directory? No good if not!
if ((!fileToRead.exists()) ||
(!fileToRead.canRead()) ||
(!fileToRead.isFile()))
{
System.out.println("try again --- file " +
fileName + " cannot be read");
}
else
{
try
{
// instantiate FileReader stream fromStream,
// use its readln() method to grab contents
// to go into fileContents
//
// do not forget to close the file!
BufferedReader fromStream =
new BufferedReader(new FileReader(fileToRead));
int newVal = Integer.parseInt(fromStream.readLine());
String newName = fromStream.readLine();
System.out.println("val read: " + newVal);
System.out.println("name read: " + newName);
this.value = newVal;
this.name = newName;
// is this "dangerous", not placing in a
// finally clause or something?
fromStream.close();
}
catch (FileNotFoundException exc)
{
// a failed opening of a file (a failed attempt
// to instantiate fromStream, above) could
// throw a FileNotFoundException
System.out.println("readFromFile: Could not open: "
+ fileName);
}
catch (IOException exc)
{
System.out.println("readFromFile: IOError: " +
exc.getMessage());
}
}
}
/*-------------------------------------------------------
saveToFile
Purpose: to demonstrate file output, AND to save the
fields of this object to file of the user's
choice. (This will put this.value on the
first line of the file, and this.name on the
second line of the file)
---------------------------------------------------------*/
public void saveToFile() throws IOException
{
// set up standard input to handle reading of
// one line at a time
BufferedReader userInput = new BufferedReader(
new InputStreamReader (System.in) );
System.out.println("please enter file name where object values");
System.out.print(" should be written: ");
String fileName = userInput.readLine();
// create a File object instance
File fileToWrite = new File(fileName);
// is this currently the name of an existing, readable file
// in the current directory? No good if not!
try
{
BufferedWriter outStream = new BufferedWriter(
new FileWriter(fileToWrite));
outStream.write("" + this.value);
outStream.newLine(); // write a line terminator
outStream.write(this.name);
outStream.close();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}