cs1705
Class IOHelper

java.lang.Object
  extended bycs1705.IOHelper

public class IOHelper
extends Object

This class provides several static methods that streamline the use of basic I/O operations in Java. It is designed to work seamlessly within the BlueJ environment, but is equally applicable in any other IDE or for standaline applications. It is designed to make it simple to use BufferedReader and PrintWriter for basic character-at-a-time and line-at-a-time input, as well as string-based output.

This class is based on Petr Skoda's "Beginner's IOHelper for BlueJ" (@href{http://www.rdv.vslib.cz/skodak}).

Version:
2003.08.20
Author:
Stephen Edwards (based on Petr Skoda's original)

Method Summary
static BufferedReader createBufferedReader(File file)
          Creates an instance of BufferedReader that can be used to read from the given file.
static BufferedReader createBufferedReader(String pathname)
          Creates an instance of BufferedReader that can be used to read from the file referred to by the given name.
static BufferedReader createBufferedReader(URL url)
          Creates an instance of BufferedReader that can be used to read from a URL over the net.
static BufferedReader createBufferedReaderForString(String s)
          Creates an instance of BufferedReader that can be used to read directly from a given text string.
static BufferedReader createBufferedReaderForURL(String url)
          Creates an instance of BufferedReader that can be used to read from a URL given as a text string.
static PrintWriter createConsoleWriter()
          Creates and returns an instance of PrintWriter that can be used to write to the console.
static BufferedReader createKeyboardReader()
          Creates and returns an instance of BufferedReader that can be used to read from the keyboard.
static PrintWriter createPrintWriter(File file, boolean append)
          Creates an instance of PrintWriter that can be used to write to the given file.
static PrintWriter createPrintWriter(String pathname)
          Creates an instance of PrintWriter that can be used to write to the file referred to by the given name.
static PrintWriter createPrintWriter(String pathname, boolean append)
          Creates an instance of PrintWriter that can be used to write to the file referred to by the given name.
static File getCurrentWorkingDirectory()
          Returns current working directory.
static File getFile(String pathname)
          Creates new File instance from a relative or an absolute pathname.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createBufferedReader

public static BufferedReader createBufferedReader(File file)
                                           throws IOException
Creates an instance of BufferedReader that can be used to read from the given file.

Parameters:
file - the file to read from
Returns:
instance of BufferedReader for reading from the file
Throws:
IOException - if there is an error opening the file

createBufferedReader

public static BufferedReader createBufferedReader(String pathname)
                                           throws IOException
Creates an instance of BufferedReader that can be used to read from the file referred to by the given name.

Parameters:
pathname - a relative or absolute pathname indicating the file to read from
Returns:
instance of BufferedReader for reading from the named file
Throws:
IOException - if there is an error opening the file

createBufferedReader

public static BufferedReader createBufferedReader(URL url)
                                           throws IOException
Creates an instance of BufferedReader that can be used to read from a URL over the net.

Parameters:
url - the URL to read from
Returns:
instance of BufferedReader for reading from the URL
Throws:
IOException - if there is an error opening the URL

createBufferedReaderForString

public static BufferedReader createBufferedReaderForString(String s)
Creates an instance of BufferedReader that can be used to read directly from a given text string.

Parameters:
s - the string to read from
Returns:
instance of BufferedReader for reading from the given string

createBufferedReaderForURL

public static BufferedReader createBufferedReaderForURL(String url)
                                                 throws IOException
Creates an instance of BufferedReader that can be used to read from a URL given as a text string.

Parameters:
url - a string denoting a URL to read from
Returns:
instance of BufferedReader for reading from the given URL
Throws:
IOException - if there is an error opening the URL or the url is syntactically incorrect

createConsoleWriter

public static PrintWriter createConsoleWriter()
Creates and returns an instance of PrintWriter that can be used to write to the console. Normally, System.out is easy enough to use for basic information. However, this method makes it easier to write I/O-based classes that use PrintWriter in their interface, but are still capable of writing to the screen as well as to a file.

Returns:
instance of PrintWriter for writing to the console

createKeyboardReader

public static BufferedReader createKeyboardReader()
Creates and returns an instance of BufferedReader that can be used to read from the keyboard. To enter text, you usually have to switch to some console in your IDE. Also, remember not to close the keyboard reader. The BufferedReader.readLine() method blocks until the user hits enter.

Returns:
instance of BufferedReader for reading from keyboard

createPrintWriter

public static PrintWriter createPrintWriter(File file,
                                            boolean append)
                                     throws IOException
Creates an instance of PrintWriter that can be used to write to the given file. If the file does not exist, it will be created if possible (including any necessary parent directories that do not exist).

Parameters:
file - the file to write to
append - true means preserve any existing file contents and start write at the end of the current file, while false means to delete any existing content first and start writing from scratch.
Returns:
instance of PrintWriter for writing to the given file
Throws:
IOException - if there is an error opening the file

createPrintWriter

public static PrintWriter createPrintWriter(String pathname)
                                     throws IOException
Creates an instance of PrintWriter that can be used to write to the file referred to by the given name. If the file does not exist, it will be created if possible (including any necessary parent directories that do not exist). If the file does exist, it will be overwritten.

Parameters:
pathname - a relative or absolute pathname indicating the file to write to
Returns:
instance of PrintWriter for writing to the given file
Throws:
IOException - if there is an error opening the file

createPrintWriter

public static PrintWriter createPrintWriter(String pathname,
                                            boolean append)
                                     throws IOException
Creates an instance of PrintWriter that can be used to write to the file referred to by the given name. If the file does not exist, it will be created if possible (including any necessary parent directories that do not exist).

Parameters:
pathname - a relative or absolute pathname indicating the file to write to
append - true means preserve any existing file contents and start write at the end of the current file, while false means to delete any existing content first and start writing from scratch.
Returns:
instance of PrintWriter for writing to the given file
Throws:
IOException - if there is an error opening the file

getCurrentWorkingDirectory

public static File getCurrentWorkingDirectory()
Returns current working directory. In BlueJ, the currect directory is undefined and can not be set, so the directory of the current project is returned instead.

Returns:
current working directory

getFile

public static File getFile(String pathname)
Creates new File instance from a relative or an absolute pathname. If given a relative path, it is resolved against the current working directory. In BlueJ, the directory of the current project is used as the working directory for resolving relative paths. Absolute path names are left unaltered.

Parameters:
pathname - a relative or absolute pathname
Returns:
instance of File referring to the specified path