Swing Java TXT文件写入Jtextarea

发布于 2025-01-21 11:51:32 字数 3291 浏览 4 评论 0原文

有没有办法在文件末尾消失?我决定在while循环之前附加第一行,以免受\ n的影响,但在这样做时出现了无效...(没有第一个附加,它只是跳过第一行,然后开始在第二行上打印)

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();
    }
}

在JTextarea中的外观

hello world
hello world
hello world
hello world
null

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2025-01-28 11:51:32

固定的! \ n应在strline之后:

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

Fixed! the \n should be after strLine:

while(strLine != null){
    theArea.append(strLine + "\n");   
    strLine = txtfile.readLine();
}
一笑百媚生 2025-01-28 11:51:32

或使它变得容易得多

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

Or make it a lot easier with

theArea.read(txtFile, ""); // Done so just your close code now
趁微风不噪 2025-01-28 11:51:32

解决方案

通过在打印前阅读而更改

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

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

错误

,您会跳过NULL检查。
您已经在循环之前从txtfile中读取一行。您应该在阅读另一张之前先打印它。
这个错误还有另一个结果 - 第一行是跳过的。
如果您的文件

a
b
c
d

作为内容,输出将是

b
c
d
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文