Please send questions to
st10@humboldt.edu .
// how hard is it to simply read from a (text) file?
//
// last modified: 11-2-00
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class TryReadFromFile
{
public static void main(String args[])
{
TryReadFromFileFrame thisFrame = new TryReadFromFileFrame();
thisFrame.setSize(600, 400);
thisFrame.setTitle("Trying to Simply Read From a File");
thisFrame.show();
}
}
class TryReadFromFileFrame extends JFrame implements ActionListener
{
JLabel title, fileToReadLabel;
JTextArea fileContents, filesList;
JScrollPane fileContentsPane, filesListPane;
JTextField fileToReadField;
String currentDirectory;
File currDirFile;
public TryReadFromFileFrame()
{
Container myContentPane;
myContentPane = getContentPane();
myContentPane.setLayout(new FlowLayout());
// gracefully handle a window closing event
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
// this text area will hold names of files in current
// directory
filesList = new JTextArea(10, 20);
// if I put this JTextArea into a JScrollPane
// in this way, then
// the scroll pane will add scroll bars when
// needed...!
filesListPane = new JScrollPane(filesList);
myContentPane.add(filesListPane);
// here's how you can get the name of
// the current working directory!
currentDirectory = System.getProperty("user.dir");
System.out.println("currentDirectory: " + currentDirectory);
// get File object for current directory
currDirFile = new File(currentDirectory);
// this local method gets the files in currDirFile and
// displays them in a JTextArea one
// file name per line
fillFilesList(currDirFile, filesList);
// this text area will hold contents of the desired
// file (within a scrollable JScrollPane)
fileContents = new JTextArea(10, 20);
fileContentsPane = new JScrollPane(fileContents);
myContentPane.add(fileContentsPane);
fileToReadLabel = new JLabel("type new file name: ");
myContentPane.add(fileToReadLabel);
fileToReadField = new JTextField(20);
fileToReadField.addActionListener(this);
myContentPane.add(fileToReadField);
}
//-----
// display the names of files in the directory corresponding
// to currDir in the text area currFilesList
//-----
private void fillFilesList(File currDir, JTextArea currFilesList)
{
String fileNamesToPrint;
String[] filesInCurrDir;
// what are the names of the files in
// the current directory, at this point?
filesInCurrDir = currDir.list();
// put each in a string, each followed by a
// newline character
fileNamesToPrint = "";
for (int i = 0; i < filesInCurrDir.length; i++)
{
fileNamesToPrint += filesInCurrDir[i] + "\n";
}
// make that the parameter text area's contents
currFilesList.setText(fileNamesToPrint);
}
// when a file name is entered into textfield,
// either complain or read file contents into
// fileContents textarea
public void actionPerformed(ActionEvent e)
{
String fileToReadName, contentsString,
latestLine;
File fileToRead;
// another java.io option... made for reading text in
// particular (as opposed to binary files), permits
// reading of a whole line at a time
BufferedReader fromStream;
fileToReadName = fileToReadField.getText();
fileToRead = new File(fileToReadName);
// 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()))
{
fileToReadField.setText("try again --- file " +
fileToReadName + " 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!
fromStream = new BufferedReader(new FileReader(fileToRead));
latestLine = fromStream.readLine();
while (latestLine != null)
{
// hey! there's an append() method
// to append to a text area!
fileContents.append(latestLine + "\n");
latestLine = fromStream.readLine();
}
// is this "dangerous", not placing in a
// finally clause or something?
fromStream.close();
fileToReadField.setText("");
}
catch (FileNotFoundException exc)
{
// a failed opening of a file (a failed attempt
// to instantiate fromStream, above) could
// throw a FileNotFoundException
System.out.println("TryReadFromFile: Could not open: "
+ fileToReadName);
}
catch (IOException exc)
{
System.out.println("TryReadFromFile: IOError: " +
exc.getMessage());
}
}
}
}