jfilechooser—如何保存选定的文件,以便下次在java中使用该程序

bpzcxfmw  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(396)

这个问题在这里已经有答案了

在java应用程序中保存用户设置的最佳方法是什么(3个答案)
14天前关门了。
我正在尝试保存最后选择的文件(由用户使用 JFilechooser )这样下次程序运行时,文件将自动打开。

public void actionPerformed(ActionEvent evt) {
    JFileChooser fileopen = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("xml files", "xml");
    fileopen.addChoosableFileFilter(filter);

    int ret = fileopen.showDialog(null, "Open file");
    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = fileopen.getSelectedFile();
        xmlSetUp(file);
        //add save file for next use
    }
}
n3schb8v

n3schb8v1#

只有对重复问题的公认答案提到了javapreferencesapi,但它不包含任何示例代码。下面的代码显示 JFrame 包含 JTextField 显示选定文件的路径以及 JButton . 当你激活 JButton ,它显示 JFileChooser . 一旦您选择了一个文件,该选择就保存为一个[java]首选项。下次运行同一应用程序时 JTextField 将最初显示保存在首选项中的路径。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Remember implements ActionListener, Runnable {
    private static final String  CHOOSE = "Choose";
    private static final String  LAST_FILE_CHOSEN = "LAST_FILE_CHOSEN";

    private JFrame  frame;
    private JTextField  textField;
    private String  savedFile;

    @Override
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case CHOOSE:
                selectFile();
                break;
            default:
                JOptionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "UNHANDLED",
                                              JOptionPane.WARNING_MESSAGE);
        }
    }

    @Override
    public void run() {
        showGui();
    }

    private JButton createButton(String text, int mnemonic, String tooltip) {
        JButton button = new JButton(text);
        button.setMnemonic(mnemonic);
        button.addActionListener(this);
        return button;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(createButton(CHOOSE, KeyEvent.VK_C, "Select file."));
        return buttonsPanel;
    }

    private JPanel createTopPanel() {
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Selected File");
        topPanel.add(label);
        textField = new JTextField(20);
        Preferences prefs = Preferences.userNodeForPackage(getClass());
        savedFile = prefs.get(LAST_FILE_CHOSEN, "");
        textField.setText(savedFile);
        topPanel.add(textField);
        return topPanel;
    }

    private void selectFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        File initial = new File(savedFile);
        if (initial.exists()) {
            fileChooser.setCurrentDirectory(initial.getParentFile());
        }
        if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
            File f = fileChooser.getSelectedFile();
            String path = f.getAbsolutePath();
            textField.setText(path);
            if (!savedFile.equals(path)) {
                Preferences prefs = Preferences.userNodeForPackage(getClass());
                prefs.put(LAST_FILE_CHOSEN, path);
            }
        }
    }

    private void showGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createTopPanel(), BorderLayout.PAGE_START);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Remember());
    }
}

相关问题