Tutorial Tutorial Step 10

Building a Java text editor

Step 9: Adding code to read text from a file

Next let's add the code that actually reads text from the user-selected file into the JTextArea.

First, you need to add a new method to your class to perform the actual open file operation. We'll call this method openFile().

  1. Switch to the editor in TextEditFrame.java and insert the following openFile() method. You can put this method anywhere in your class (outside of other methods). A good place for it is just after the code for the jbInit() method, just before the jMenuFileExit_actionPerformed() event.
    // Open named file; read text from file into jTextArea1; report to statusBar.
    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 JTextArea.
        jTextArea1.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);
      }
    }
    
  2. Add the following import to the list of imports at the top of the file:
    import java.io.*;
  3. Click the File|Open event handler (jMenuItem2_actionPerformed(ActionEvent)) in the structure pane to locate it in the source code.

  4. Replace the code in the File|Open event handler if() statement that previously said:
    // Display the name of the opened directory+file in the statusBar.
    statusBar.setText("Opened "+jFileChooser1.getSelectedFile().getPath());
    
    // Code will need to go here to actually load text
    // from file into JTextArea.
    
    with this new openFile() method instead, using the concatenated Directory and File name.
    // Call openFile to attempt to load the text from file into JTextArea
    openFile(jFileChooser1.getSelectedFile().getPath());
    //repaints menu after item is selected
    this.repaint();
    
  5. Now try it out and see if it works. Save and run your program and open a text file in your editor. You should have contents in the text editor.

  6. Close the "Text Editor" application.

Tutorial Tutorial Step 10