All pastes #274315 Raw Edit

Untitled

public text v1 · immutable
·published 2006-12-10 16:51 UTC
rendered paste body
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
 *  The Disk class supports basic text file I/O capabilities.
 */
public class Disk
{
    private File outfile;
/**
 *  use this PrintWriter for printing. For example, disk.out.println("Hello")
 */
    public PrintWriter out;
    private File infile;
    private BufferedReader in;

/**
 *  @param filename the name of a textfile 
 *  @param io must be either "in" (for reading) or "out" (for writing)
 */
    public Disk (String filename, String io)
    {
	// sets up a PrintWriter for writing to text files
	if (io.equals ("out"))
	{
	    outfile = new File (filename);
	    try
	    {
		out = new PrintWriter (new FileWriter (outfile));
	    }
	    catch (Exception e1)
	    {
		System.out.println ("Disk Constructor: error");
	    }
	}
	// sets up a BufferedReader reading from text files
	if (io.equals ("in"))
	{
	    infile = new File (filename);
	    try
	    {
		in = new BufferedReader (new FileReader (infile));
	    }
	    catch (FileNotFoundException e2)
	    {
		System.out.println (filename + " not found");
	    }

	}
    } // end Disk constructor

/**
    @return a string corresponding to the next unread line of an input file
 */
    public String readLine ()
    {
	String line = null;
	try
	{
	    line = in.readLine (); // this can throw an exception
	}


	catch (java.io.IOException e)
	{
	    System.out.println ("input line not valid");
	}


	return line;
    } // end readLine
}