如何在 Java 中保存所选文件以备下次使用程序
How to save a chosen File for the next use of the Program in Java
我正在尝试保存最后选择的文件(由用户使用 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
}
}
只有 duplicate question 的已接受答案提到了 Java 首选项 API,但它不包含任何示例代码。下面的代码显示一个 JFrame
,其中包含一个 JTextField
,显示 selected 文件的路径以及一个 JButton
。当您激活 JButton
时,它会显示一个 JFileChooser
。一旦你 select 一个文件,那个 selection 就会被保存为 [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());
}
}
我正在尝试保存最后选择的文件(由用户使用 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
}
}
只有 duplicate question 的已接受答案提到了 Java 首选项 API,但它不包含任何示例代码。下面的代码显示一个 JFrame
,其中包含一个 JTextField
,显示 selected 文件的路径以及一个 JButton
。当您激活 JButton
时,它会显示一个 JFileChooser
。一旦你 select 一个文件,那个 selection 就会被保存为 [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());
}
}