如何将文本文件内容导入到 Java 应用程序中的 JTextArea?

发布于 2024-11-28 22:55:59 字数 58 浏览 1 评论 0原文

如何使用 JFileChooser 将文本文件内容导入到 Java 应用程序中的 JTextArea?

how to import a Text file content to a JTextArea in a Java application using JFileChooser?

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

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

发布评论

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

评论(5

最舍不得你 2024-12-05 22:55:59

应该类似于下面的代码:

JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null); //replace null with your swing container
File file;
if(returnVal == JFileChooser.APPROVE_OPTION)     
  file = chooser.getSelectedFile();    
}

JTextArea text = new JTextArea();
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
  text.append(line + "\n");
  line = in.readLine();
}

基本逻辑:

BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
  text.append(line + "\n");
  line = in.readLine();
}

should be something like the following code:

JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null); //replace null with your swing container
File file;
if(returnVal == JFileChooser.APPROVE_OPTION)     
  file = chooser.getSelectedFile();    
}

JTextArea text = new JTextArea();
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
  text.append(line + "\n");
  line = in.readLine();
}

The basic logic:

BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
  text.append(line + "\n");
  line = in.readLine();
}
_畞蕅 2024-12-05 22:55:59

文档查看器

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

class DocumentViewer {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame f = new JFrame("Document Viewer");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JFileChooser fileChooser = new JFileChooser();

                JPanel gui = new JPanel(new BorderLayout());

                final JEditorPane document = new JEditorPane();
                gui.add(new JScrollPane(document), BorderLayout.CENTER);

                JButton open = new JButton("Open");
                open.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        int result = fileChooser.showOpenDialog(f);
                        if (result==JFileChooser.APPROVE_OPTION) {
                            File file = fileChooser.getSelectedFile();
                            try {
                                document.setPage(file.toURI().toURL());
                            } catch(Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                gui.add(open, BorderLayout.NORTH);

                f.setContentPane(gui);
                f.pack();
                f.setSize(400,300);
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        });
    }
}

Document Viewer

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

class DocumentViewer {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame f = new JFrame("Document Viewer");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JFileChooser fileChooser = new JFileChooser();

                JPanel gui = new JPanel(new BorderLayout());

                final JEditorPane document = new JEditorPane();
                gui.add(new JScrollPane(document), BorderLayout.CENTER);

                JButton open = new JButton("Open");
                open.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        int result = fileChooser.showOpenDialog(f);
                        if (result==JFileChooser.APPROVE_OPTION) {
                            File file = fileChooser.getSelectedFile();
                            try {
                                document.setPage(file.toURI().toURL());
                            } catch(Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                gui.add(open, BorderLayout.NORTH);

                f.setContentPane(gui);
                f.pack();
                f.setSize(400,300);
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        });
    }
}
和我恋爱吧 2024-12-05 22:55:59

要将文件的内容导入 JTextArea,您只需按照以下步骤操作即可!

  1. 创建一个框架并向其中添加一个 JTextArea。
  2. 您声明并初始化 JFileChooser。
  3. 您向 JFileChooser 添加一个侦听器。
  4. 在您的 actionPerformed 中,您应该获取所选的文件并将其传递给读取该文件的方法(请参见下面的注意事项)。
  5. 在该方法中,您打开文件阅读器并逐行读取文件的内容。当您这样做时,您将每一行附加到 JTextArea。
  6. 当您到达文件末尾时,关闭文件阅读器。
  7. 运行该程序,您应该就可以开始了。

上述步骤足以执行您的任务。但是,当您尝试时,我会编辑我的帖子并添加可能的解决方案。

注意:您必须注意,当您使用 JFileChooser 选择文件时,它会返回 File 类型的对象。然后,您应该使用 File 类提供的 getName() 方法来获取文件的名称。

可能有帮助的链接!
JFileChooser
文件
有关如何使用 JFileChooser 的 Java 教程

To import the contents of a file into a JTextArea you simply follow these steps!

  1. Create a frame and add a JTextArea to it.
  2. You declare and initialize a JFileChooser.
  3. You add a listener to the JFileChooser.
  4. In your actionPerformed, you should take the file that was selected and pass it to a method that would read this file(see NB below).
  5. In that method, you open a file reader and read the contents of the file, line by line. As you do so, you append each line to the JTextArea.
  6. When you get to the end of the file, you close the file reader.
  7. Run the program and you should be good to go.

The above steps are good enough to perform your task. However, when you give it a try, i would edit my post and add a possible solution.

NB: You must note that when you select a file with a JFileChooser, it returns an Object of type File. You should then use the getName() method provided by the File class to get the name of the file.

Links that might be of help!
JFileChooser
File
Java tutorials on how to use the JFileChooser

遮了一弯 2024-12-05 22:55:59

确定从 FileChooser 给出的文件名,将文件的内容读入字符串(例如使用 StringBuilder),使用 <代码> JTextField#setText(String)

Determine the filename given from the FileChooser, read the contents of the file into a String (e.g. using a StringBuilder), set the contents of the JTextArea to the contents of the buffer using JTextField#setText(String).

缘字诀 2024-12-05 22:55:59
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser jf = new JFileChooser();
     final JEditorPane document = new JEditorPane();
    int returnval=jf.showDialog(this, null);
    File file = null;
    if(returnval == JFileChooser.APPROVE_OPTION)     
     file = jf.getSelectedFile(); 
    String str ;
    try {
        byte bt[]= Files.readAllBytes(file.toPath());   
        str=new String(bt,"UTF-8");
        System.out.println(str);
    } catch (IOException ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    }
}  
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser jf = new JFileChooser();
     final JEditorPane document = new JEditorPane();
    int returnval=jf.showDialog(this, null);
    File file = null;
    if(returnval == JFileChooser.APPROVE_OPTION)     
     file = jf.getSelectedFile(); 
    String str ;
    try {
        byte bt[]= Files.readAllBytes(file.toPath());   
        str=new String(bt,"UTF-8");
        System.out.println(str);
    } catch (IOException ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文