java - JFileChooser.SetCurrentDirectory 不工作
问题描述
我有一个 JFileChooser,我想使用存储在 .txt 文件中的一些信息来设置它打开的目录(我正在使用 .txt 文件在会话之间保留所需的位置)。我可以获取文件,读取数据并将其设置为字符串,但是当我尝试使用该字符串来设置要打开的目录时,它不起作用。我的代码大致是这样的:
//buffer contains a byte[] for "/Users/user/Documents/Work/folderToOpen"
desiredPath = new String(buffer);
jFileChooser1.setCurrentDirectory(new java.io.File(desiredPath));
但是,在逐步完成此操作后,当前目录设置为 /Users/user。
如果有人对我做错了什么有任何想法,或者有更好的方法来实现这一点,我很想听听。
谢谢
private static String LAST_FOLDER_USED = null;
//Get the desired file path for user preferences
String pluginRoot = System.getProperty("user.dir") + File.separator.toString();
//Create a file using the desired file Path
File userPreferences = new File(pluginRoot + File.separator + "UserPreferences.txt");
//Get a file called UserPreferences.txt from target/classes to create an input stream
String fileName = "UserPreferences.txt";
InputStream readInFile = getClass().getResourceAsStream(fileName);{
//Convert input stream to read from the desired file in the plug-in root ("filePath" Created Above)
try{
readInFile = new FileInputStream(userPreferences);
}
catch (IOException e){
e.printStackTrace();
}}
//Read the readInFile into a byte[]
String desiredPathToOpenImage;
byte[] buffer = new byte[1000];
int i = 0;{
try {
while((i = readInFile.read(buffer)) !=-1){
System.out.println(new String(buffer));
i++;
}}
catch (IOException e) {
e.printStackTrace();
};
//Convert byte[] to string (This should be the path to the desired folder when selecting an image)
desiredPathToOpenImage = new String(buffer);
}
//Create a New File using the desired path
File desiredPath = new File(desiredPathToOpenImage + File.separator + "prefs.txt");
public SelectImage(Viewer parent, boolean modal) {
super(parent, modal);
initComponents();
int returnVal = jFileChooser1.showOpenDialog(parent);
// Sets up arrays for storing file information to be passed back to the viewer class.
String[] filePath = new String[jFileChooser1.getSelectedFiles().length];
String[] fileName = new String[jFileChooser1.getSelectedFiles().length];
String[] fileDir = new String[jFileChooser1.getSelectedFiles().length];
if (returnVal == JFileChooser.APPROVE_OPTION) {
// Cycles through the selected files and stores each piece accordingly
for (int i = 0; i < jFileChooser1.getSelectedFiles().length; i++) {
File file = jFileChooser1.getSelectedFiles()[i];
filePath[i] = file.getPath();
fileName[i] = file.getName();
fileDir[i] = file.getParent();
}
}
parent.setFilePath(filePath, fileName, fileDir);
}
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jFileChooser1.setMultiSelectionEnabled(true);
//Checks folder_Path to see if a value is present. If value is present sets jFileChooser Directory to that value
if(desiredPathToOpenImage.contains(File.separator)){
//Create a File using the desired path for selecting images
//****Currently doesn't set the Directory correctly****//
jFileChooser1.setCurrentDirectory(desiredPath);
}
//If no value is present in LAST_FOLDER_USED sets jFileChooser Directory to desktop
else{
jFileChooser1.setCurrentDirectory(new java.io.File("/Users/benwoodruff/Desktop"));
}
jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooser1ActionPerformed(evt);
//After file is selected sets value of LAST_FOLDER_USED to the absolute path of that file
LAST_FOLDER_USED = jFileChooser1.getCurrentDirectory().toString() + File.separator + "UserPreferences.txt";
try {
FileWriter fileWriter = new FileWriter(userPreferences);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(jFileChooser1.getCurrentDirectory().toString());
OutputStream outPut = new FileOutputStream(pluginRoot + File.separator + "UserPreferences.txt");
outPut.write(LAST_FOLDER_USED.getBytes());
outPut.close();
bufferedWriter.close();
} catch (IOException e) {
System.out.println("Error Writing to File" + desiredPathToOpenImage);
e.printStackTrace();
}
}
});
解决方案
我认为作为参数传递的目录不存在或您登录的用户无法访问,从 setCurrentDirectory() 的 javadoc 判断:
如果作为 currentDirectory 传入的文件不是目录,则文件的父级将用作 currentDirectory。如果父级不可遍历,那么它将向上遍历父级树,直到找到可遍历的目录,或者到达文件系统的根目录。
确保给定路径中的所有文件夹都存在并且登录用户可以访问(在 linux 上,“可执行”位控制目录的可访问性)。所以如果你看到类似的东西
-dx 文件
执行后
ls -l *
在 shell 中,可以访问 Documents 目录。
推荐阅读
- ios - How do I wrap the viewDidLoad function?
- jboss - wildfly authentication lost at login redirection
- multithreading - Get the ID of the currently executing CPU on M1
- mysql - Select top 10 json items from json array (in cloumn data) in MYSQL?
- javascript - 带有 rxjs 的 vanilla js:自动完成搜索
- firebase - 如何检查用户的登录?
- sql-server - SSIS 数据转换字符串到时间戳
- javascript - 未捕获的类型错误:无法将属性“onclick”设置为 null。重叠的 JavaScript
- azure - 无法使用 VS Code 将 Azure Function Apps 发布到 Azure 服务环境
- python - Tkinter- 索引不使用插入方法工作