public class IOHelper extends Object
Scanner
and
PrintWriter
for basic input and string-based output.
This class is based on Petr Skoda's "Beginner's IOHelper for BlueJ" (@href{http://www.rdv.vslib.cz/skodak}).
Modifier and Type | Method and Description |
---|---|
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 Scanner |
createKeyboardScanner()
Creates and returns a
Scanner 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 Scanner |
createScanner(File file)
Creates a
Scanner that can be used
to read from the given file. |
static Scanner |
createScanner(String pathname)
Creates a
Scanner that can be used to
read from the file referred to by the given name. |
static Scanner |
createScanner(URL url)
Creates a
Scanner that can be used
to read from a URL over the net. |
static Scanner |
createScannerForString(String s)
Creates a
Scanner that can be used to
read directly from a given text string. |
static Scanner |
createScannerForURL(String url)
Creates a
Scanner that can be used to
read from a URL given as a text string. |
static File |
getCurrentWorkingDirectory()
Returns current working directory.
|
static File |
getFile(File pathname)
Takes an existing file and, if it contains a relative path name,
it is resolved against the current working directory.
|
static File |
getFile(String pathname)
Creates a new
File instance from a relative or an absolute
pathname. |
static void |
setCurrentWorkingDirectory(File newCwd)
Changes the current working directory (for the purposes of future
calls to IOHelper methods).
|
public static File getCurrentWorkingDirectory()
public static void setCurrentWorkingDirectory(File newCwd)
newCwd
- The new current working directorypublic static File getFile(String pathname)
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.pathname
- a relative or absolute pathnameFile
referring to the
specified pathpublic static File getFile(File pathname)
pathname
- a relative or absolute pathname as a File objectFile
referring to the
specified pathpublic static BufferedReader createKeyboardReader()
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.
This method is provided to support older code, but writers of newer
code should consider createKeyboardScanner()
instead.
BufferedReader
for reading from keyboardpublic static Scanner createKeyboardScanner()
Scanner
that
can be used to read from the keyboard. This is just a convenience
method for uniformity, since the Scanner class provides a constructor
that can take System.in
as a parameter.
To enter text, you usually have to switch to some console in your
IDE. Also, remember not to close the keyboard scanner. The Scanner.nextLine()
method blocks until the user hits enter.Scanner
for reading from keyboardpublic static BufferedReader createBufferedReader(File file)
BufferedReader
that can be used
to read from the given file.
This method is provided to support older code, but writers of newer
code should consider createScanner(File)
instead.
file
- the file to read fromBufferedReader
for reading from
the fileRuntimeException
- if there is an error opening the filepublic static Scanner createScanner(File file)
Scanner
that can be used
to read from the given file. This is just a convenience
method for uniformity, since the Scanner class provides a constructor
that can take System.in
as a parameter.
Turns any IOException
that is raised into a
RuntimeException
so that it does not have to be
placed inside a try/catch block.file
- the file to read fromScanner
for reading from
the filepublic static BufferedReader createBufferedReader(String pathname)
BufferedReader
that can be used to
read from the file referred to by the given name.
This method is provided to support older code, but writers of newer
code should consider createScanner(String)
instead.
pathname
- a relative or absolute pathname indicating the
file to read fromBufferedReader
for reading from
the named fileRuntimeException
- if there is an error opening the filepublic static Scanner createScanner(String pathname)
Scanner
that can be used to
read from the file referred to by the given name.
Turns any IOException
that is raised into a
RuntimeException
so that it does not have to be
placed inside a try/catch block.pathname
- a relative or absolute pathname indicating the
file to read fromScanner
for reading from
the named filepublic static BufferedReader createBufferedReader(URL url)
BufferedReader
that can be used
to read from a URL over the net.
This method is provided to support older code, but writers of newer
code should consider createScanner(URL)
instead.
url
- the URL to read fromBufferedReader
for reading from
the URLRuntimeException
- if there is an error opening the URLpublic static Scanner createScanner(URL url)
Scanner
that can be used
to read from a URL over the net.
Turns any IOException
that is raised into a
RuntimeException
so that it does not have to be
placed inside a try/catch block.url
- the URL to read fromScanner
for reading from
the URLpublic static BufferedReader createBufferedReaderForURL(String url)
BufferedReader
that can be used to
read from a URL given as a text string.
This method is provided to support older code, but writers of newer
code should consider createScannerForURL(String)
instead.
url
- a string denoting a URL to read fromBufferedReader
for reading from
the given URLRuntimeException
- if there is an error opening the URL or the
URL is syntactically incorrectpublic static Scanner createScannerForURL(String url)
Scanner
that can be used to
read from a URL given as a text string.
Turns any MalformedURLException
or other IOException
that is raised into a RuntimeException
so that it does not
have to be placed inside a try/catch block.url
- a string denoting a URL to read fromScanner
for reading from
the given URLpublic static BufferedReader createBufferedReaderForString(String s)
BufferedReader
that can be used to
read directly from a given text string.
This method is provided to support older code, but writers of newer
code should consider createScannerForString(String)
instead.
s
- the string to read fromBufferedReader
for reading from
the given stringpublic static Scanner createScannerForString(String s)
Scanner
that can be used to
read directly from a given text string. This is just a convenience
method for uniformity, since the Scanner class provides a constructor
that can take a String as a parameter.s
- the string to read fromScanner
for reading from
the given stringpublic static PrintWriter createConsoleWriter()
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.PrintWriter
for writing to the consolepublic static PrintWriter createPrintWriter(File file, boolean append)
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).file
- the file to write toappend
- 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.PrintWriter
for writing to the
given filepublic static PrintWriter createPrintWriter(String pathname, boolean append)
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).pathname
- a relative or absolute pathname indicating the file
to write toappend
- 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.PrintWriter
for writing to the
given filepublic static PrintWriter createPrintWriter(String pathname)
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.pathname
- a relative or absolute pathname indicating the file
to write toPrintWriter
for writing to the
given file