我想获取用户在Java中保存的文件的完整路径。 这是方法保存的代码,它工作正常但实际上我需要获取用户保存文件的路径。有人可以帮助我:
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class FileChooserSave {
private static void createAndShowUI() {
final JFileChooser chooser = new JFileChooser(new File(".")) {
public void approveSelection() {
if (getSelectedFile().exists()) {
int n = JOptionPane.showConfirmDialog(this, "Do You Want to Overwrite File?", "Confirm Overwrite",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION)
super.approveSelection();
} else
super.approveSelection();
}
};
chooser.setSelectedFile(new File(""));
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getSelectedFile());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
答案 0 :(得分:2)
您可以使用此方法获取绝对路径:
myFileChooser.getSelectedFile().getAbsolutePath()
答案 1 :(得分:2)
以下是来自JFileChoose
的绝对路径的Java代码示例JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
希望它有所帮助...