简体   繁体   中英

Swing Java txt file writing to JTextArea

Is there a way to make the null go away at the end of the file? I decided to append the first line before the while loop so it wouldn't be affected by the \n but in doing so the null appears... (without the first append it just skips the first line and starts printing on the second line)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;

public class notepadMinus implements ActionListener{
    //Properties
    JFrame theFrame;
    JPanel thePanel;
    JMenuBar theBar;
    JMenu theMenu;
    JMenuItem openItem;
    JMenuItem saveItem;
    JTextArea theArea;
    JScrollPane theScroll;
    JFileChooser openChoose;
    String strLine;
    //Methods
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource() == openItem){
            System.out.println("Open File");
            int intResult = openChoose.showOpenDialog(openItem);
            if(intResult == JFileChooser.APPROVE_OPTION){
                System.out.println("File selected");
                File selectedFile = openChoose.getSelectedFile();
                theArea.selectAll();
                theArea.replaceSelection("");
                try{
                    BufferedReader txtfile = new BufferedReader(new FileReader(openChoose.getSelectedFile()));
                    strLine = txtfile.readLine();
                    theArea.append(strLine);
                    while(strLine != null){
                        strLine = txtfile.readLine();
                        theArea.append( "\n" +  strLine);   
                    }
                    txtfile.close();
                }catch(FileNotFoundException e){
                    System.out.println("File Not Found");
                }catch(IOException e){
                    System.out.println("Reading Error");
                }   
                //read lines
                //put those lines in theArea - append
                //System.out.println(openChoose.getSelectedFile());
            }else{
                System.out.println("Cancelled?");
            }
        }
    }
    //Constructor
    notepadMinus(){
        theFrame = new JFrame("Notepad Minus");
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        thePanel = new JPanel();
        thePanel.setLayout(null);
        thePanel.setPreferredSize(new Dimension(800, 600));
        
        theBar = new JMenuBar();
        //theBar.setSize(600,50);
        //theBar.setLocation(100, 50);
        //thePanel.add(theBar);
        theFrame.setJMenuBar(theBar);
        
        theMenu = new JMenu("File");
        theBar.add(theMenu);
        
        openItem = new JMenuItem("Open");
        openItem.addActionListener(this);
        theMenu.add(openItem);
        
        saveItem = new JMenuItem("Save");
        saveItem.addActionListener(this);
        theMenu.add(saveItem);
        
        theArea = new JTextArea();
        theScroll = new JScrollPane(theArea);
        theScroll.setPreferredSize(new Dimension(800, 600));
        
        //theFrame.setContentPane(thePanel);
        theFrame.setContentPane(theScroll);
        theFrame.pack();
        theFrame.setVisible(true);
        
        openChoose = new JFileChooser();
        
    }
    //Main Method
    public static void main(String[] args){
        new notepadMinus();
    }
}

What it looks like in the JTextArea

hello world
hello world
hello world
hello world
null

The solution

Change

theArea.append(strLine);
while(strLine != null){
    strLine = txtfile.readLine();
    theArea.append( strLine + "\n" );   
}

to

while(strLine != null){
    theArea.append( "\n" +  strLine);   
    strLine = txtfile.readLine();
}

The mistake

By reading just before printing, you are skipping the null check.
You are already reading a line from txtfile before the loop. You should print it first before you read another.
There is another consequence of this mistake - the first line is skipped. If your file had

a
b
c
d

as its contents, the output would be

b
c
d
null

Fixed! the \n should be after strLine :

while(strLine != null){
    theArea.append(strLine + "\n");   
    strLine = txtfile.readLine();
}

Or make it a lot easier with

theArea.read(txtFile, ""); // Done so just your close code now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM