Java 使用filechooser加载文本文件

Java 使用filechooser加载文本文件,java,Java,“文件”菜单应该能够保存或打开现有的二进制文本文件进行文本编辑,现在我想使用filechooser如何加载现有文本文件以使用文件选择器将该文件的文本传递给我的jlabel。以下是我到目前为止所做的 大宗报价/* 使用JFileChooser: } else if (e.getSource() == openitem) { int answer = fc.showOpenDialog(null); // (JFrame.this) when in JFrame if (answer

“文件”菜单应该能够保存或打开现有的二进制文本文件进行文本编辑,现在我想使用
filechooser
如何加载现有文本文件以使用文件选择器将该文件的文本传递给我的jlabel。以下是我到目前为止所做的

大宗报价/*

使用JFileChooser:

} else if (e.getSource() == openitem) {
    int answer = fc.showOpenDialog(null); // (JFrame.this) when in JFrame
    if (answer == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        byte[] content = Files.readAllBytes(file.toPath());
        String text = new String(content); // Using default encoding
        jLabel1.setText(text);
    }

您可能希望在JScrollPane中使用JTextPane而不是JLabel。

为什么不尝试在google上搜索一些“java JFileChooser示例”83.300结果,如果遇到问题,请返回您尝试过的相关代码。您发布的代码有什么问题?你真正的问题是什么?
} else if (e.getSource() == openitem) {
    int answer = fc.showOpenDialog(null); // (JFrame.this) when in JFrame
    if (answer == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        byte[] content = Files.readAllBytes(file.toPath());
        String text = new String(content); // Using default encoding
        jLabel1.setText(text);
    }