/** * Open named file; read text from file into textArea1; report to statusBar. * @param fileName the name of the text file on disk */ void openFile(String fileName) { try { // Open a file of the given name. File file = new File(fileName); // Get the size of the opened file. int size = (int)file.length(); // Set to zero a counter for counting the number of // characters that have been read from the file. int chars_read = 0; // Create an input reader based on the file, so we can read its data. // FileReader handles international character encoding conversions. FileReader in = new FileReader(file); // Create a character array of the size of the file, // to use as a data buffer, into which we will read // the text data. char[] data = new char[size]; // Read all available characters into the buffer. while(in.ready()) { // Increment the count for each character read, // and accumulate them in the data buffer. chars_read += in.read(data, chars_read, size - chars_read); } in.close(); // Create a temporary string containing the data, // and set the string into the TextArea. textArea1.setText(new String(data, 0, chars_read)); // Display the name of the opened directory+file in the statusBar. statusBar.setText("Opened "+fileName); } catch (IOException e) { statusBar.setText("Error opening "+fileName); } }
import java.io.*;
// Display the name of the opened directory+file in the statusBar. statusBar.setText("Opened "+filer1.getDirectory()+filer1.getFile()); // Code will need to go here to actually load text // from file into TextArea.with this new openFile() method instead, using the concatenated Directory and File name.
// Call openFile to attempt to load the text from file into TextArea openFile(filer1.getDirectory()+filer1.getFile());