Java JFileChooser.SetCurrentDirectory不工作

Java JFileChooser.SetCurrentDirectory不工作,java,swing,jfilechooser,Java,Swing,Jfilechooser,我有一个JFileChooser,我想使用存储在.txt文件中的一些信息设置它打开的目录(我正在使用.txt文件在会话之间保留所需的位置)。我可以获取文件、读取数据并将其设置为字符串,但当我尝试使用该字符串设置要打开的目录时,它不起作用。我的代码大致如下: //缓冲区包含“/Users/user/Documents/Work/folderToOpen”的字节[] desiredPath=新字符串(缓冲区); jFileChooser1.setCurrentDirectory(新的java.io.

我有一个JFileChooser,我想使用存储在.txt文件中的一些信息设置它打开的目录(我正在使用.txt文件在会话之间保留所需的位置)。我可以获取文件、读取数据并将其设置为字符串,但当我尝试使用该字符串设置要打开的目录时,它不起作用。我的代码大致如下:

//缓冲区包含“/Users/user/Documents/Work/folderToOpen”的字节[]
desiredPath=新字符串(缓冲区);
jFileChooser1.setCurrentDirectory(新的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();
    }     

  }
});
private static String LAST\u FOLDER\u USED=null;
//获取用户首选项所需的文件路径
字符串pluginRoot=System.getProperty(“user.dir”)+File.separator.toString();
//使用所需的文件路径创建文件
File userPreferences=新文件(pluginRoot+File.separator+“userPreferences.txt”);
//从目标/类获取名为UserPreferences.txt的文件,以创建输入流
字符串fileName=“UserPreferences.txt”;
InputStream readInFile=getClass().getResourceAsStream(文件名);{
//将输入流转换为从插件根目录中的所需文件读取(“上面创建的文件路径”)
试一试{
readInFile=newfileinputstream(userPreferences);
}
捕获(IOE异常){
e、 printStackTrace();
}}
//将readInFile读入一个字节[]
需要的字符串路径打开图像;
字节[]缓冲区=新字节[1000];
int i=0;{
试一试{
而((i=readinfle.read(buffer))!=-1){
System.out.println(新字符串(缓冲区));
i++;
}} 
捕获(IOE异常){
e、 printStackTrace();
};
//将字节[]转换为字符串(这应该是选择图像时所需文件夹的路径)
desiredPathToOpenImage=新字符串(缓冲区);
}
//使用所需路径创建新文件
File desiredPath=新文件(desiredPathToOpenImage+File.separator+“prefs.txt”);
public SelectImage(查看器父级,布尔模式){
超级(父级、模态);
初始化组件();
int returnVal=jFileChooser1.showOpenDialog(父级);
//设置用于存储要传递回查看器类的文件信息的数组。
String[]filePath=新字符串[jFileChooser1.getSelectedFiles().length];
字符串[]文件名=新字符串[jFileChooser1.getSelectedFiles().length];
String[]fileDir=新字符串[jFileChooser1.getSelectedFiles().length];
if(returnVal==JFileChooser.APPROVE_选项){
//循环浏览选定的文件,并相应地存储每个文件
对于(int i=0;i
从setCurrentDirectory()的javadoc判断,我认为作为参数传递的目录不存在或您登录的用户无法访问:

如果作为currentDirectory传入的文件不是目录,则该文件的父级将用作currentDirectory。如果父级不可遍历,则它将沿着父级树遍历,直到找到可遍历的目录,或到达文件系统的根目录

确保给定路径中的所有文件夹都存在,并且登录用户可以访问(在linux上,“可执行”位控制目录的可访问性)

-d x文档

执行后

ls -l *

在shell中,可以访问Documents目录。

找到了一种更好的方法,使用
首选项来实现我的目标,而不是尝试创建和访问文件来存储位置

Preferences prefs = Preferences.userNodeForPackage(this.getClass());
static String LAST_FOLDER_USED = "LAST_FOLDER_USED";
String folder_Location;
然后在initComponents()中


我的猜测是,您认为正在发生的事情是不可能发生的。可能是在您认为问题不存在而没有发布的代码部分。我建议您发布其余代码。@Abra好主意,但代码应该是(特定)形式的。提示: if(LAST_FOLDER_USED != null){ jFileChooser1.setCurrentDirectory(new File(prefs.get(LAST_FOLDER_USED, LAST_FOLDER_USED))); } 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); folder_Location = jFileChooser1.getCurrentDirectory().toString(); prefs.put(LAST_FOLDER_USED, folder_Location); //System.out.println(prefs.get(LAST_FOLDER_USED, folder_Location)); } });