edu.neu.ccs.gui
Class TextFileIO

java.lang.Object
  extended byedu.neu.ccs.gui.TextFileIO

public class TextFileIO
extends Object

Class TextFileIO contains methods to read an entire text file into a string and to save a string as the content of a text file.

The basic methods use file dialogs. The most complex methods assume a File object has been defined and associated with a text file and then work with that object.

An instance retains information about the extension, file filter, and most recent directory visited by its file chooser internal object.

See also:

Version:
2.5.0

Field Summary
protected  String extension
          The file extension to filter files.
protected  JFileChooser filechooser
          The file chooser used for file I/O by this object.
protected  File home
          Initialization for home directory as directory ".".
protected  FileExtensionFilter textFileFilter
          The file filter for file I/O.
 
Constructor Summary
TextFileIO()
          The constructor that uses no file extension and therefore may read or save any text file using file dialogs.
TextFileIO(String extension)
          The constructor that can read or save text files that may be filtered using the optional file extension to filter the files shown in the file dialogs.
 
Method Summary
 String readDataFromFile()
          Opens a file dialog to get the file name of a text file and then reads all data into a String that is returned.
 String readDataFromFile(boolean displayErrorDialogs)
          Opens a file dialog to get the file name of a text file and then reads all data into a String that is returned.
 String readDataFromFile(File source, boolean displayErrorDialogs)
          Reads the text data from the given source text file and returns that data in a String.
 boolean saveDataToFile(File target, String data, boolean displayErrorDialogs)
          Saves the given text data to the file.
 boolean saveDataToFile(String data)
          Opens a file dialog to get the file name of a file and then saves the given text data to the file.
 boolean saveDataToFile(String data, boolean displayErrorDialogs)
          Opens a file dialog to get the file name of a file and then saves the given text data to the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

extension

protected String extension
The file extension to filter files. Do not include the period character.


textFileFilter

protected FileExtensionFilter textFileFilter
The file filter for file I/O.


home

protected File home
Initialization for home directory as directory ".".


filechooser

protected JFileChooser filechooser
The file chooser used for file I/O by this object.

Constructor Detail

TextFileIO

public TextFileIO()

The constructor that uses no file extension and therefore may read or save any text file using file dialogs.


TextFileIO

public TextFileIO(String extension)

The constructor that can read or save text files that may be filtered using the optional file extension to filter the files shown in the file dialogs.

Uses no file extension and no file filter if the given extension is null.

The extension, if supplied, should not contain a period.

Parameters:
extension - the optional file extension
Method Detail

readDataFromFile

public String readDataFromFile()

Opens a file dialog to get the file name of a text file and then reads all data into a String that is returned.

Displays error dialogs.

Returns null if an errors occur.

Returns:
the file content as a String or null

readDataFromFile

public String readDataFromFile(boolean displayErrorDialogs)

Opens a file dialog to get the file name of a text file and then reads all data into a String that is returned.

Displays error dialogs if the last parameter is set to true.

Returns null if an errors occur.

Returns:
the file content as a String or null

readDataFromFile

public String readDataFromFile(File source,
                               boolean displayErrorDialogs)

Reads the text data from the given source text file and returns that data in a String.

Displays error dialogs if the last parameter is set to true.

Returns null if an errors occur.

Parameters:
source - the data source
displayErrorDialogs - if true display error dialogs
Returns:
the file content as a String or null

saveDataToFile

public boolean saveDataToFile(String data)

Opens a file dialog to get the file name of a file and then saves the given text data to the file.

Displays error dialogs.

Does no save if any errors occur.

Parameters:
data - the data to save
Returns:
true if the operation succeeds

saveDataToFile

public boolean saveDataToFile(String data,
                              boolean displayErrorDialogs)

Opens a file dialog to get the file name of a file and then saves the given text data to the file.

Displays error dialogs if the last parameter is set to true.

Does no save if any errors occur.

Parameters:
data - the data to save
displayErrorDialogs - if true display error dialogs
Returns:
true if the operation succeeds

saveDataToFile

public boolean saveDataToFile(File target,
                              String data,
                              boolean displayErrorDialogs)

Saves the given text data to the file.

Displays error dialogs if the last parameter is set to true.

Does no save if any errors occur.

Parameters:
target - the target file
data - the data to save
displayErrorDialogs - if true display error dialogs
Returns:
true if the operation succeeds