我只想将目录设置为我之前写在文件中的路径。
因此我使用了:
fileChooser.setCurrentDirectory(new File("path.txt"));
在 path.txt 中给出了路径。但不幸的是,这行不通,我想知道为什么:P。我想我完全错了setCurrentDic
..
我只想将目录设置为我之前写在文件中的路径。
因此我使用了:
fileChooser.setCurrentDirectory(new File("path.txt"));
在 path.txt 中给出了路径。但不幸的是,这行不通,我想知道为什么:P。我想我完全错了setCurrentDic
..
setCurrentDirectory
takes a file representing a directory as parameter. Not a text file where a path is written.
To do what you want, you have to read the file "path.txt", create a File object with the contents that you just read, and pass this file to setCurrentDirectory :
String pathWrittenInTextFile = readFileAsString(new File("path.txt"));
File theDirectory = new File(pathWrittenInTextFile);
fileChooser.setCurrentDirectory(theDirectory);
You have to read the contents of path.txt
. Thea easiest way is through commons-io:
String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File dir = new File(fileContents);
You can also use FileUtils.readFileToString(..)
JFileChooser chooser = new JFileChooser();
try {
// Create a File object containing the canonical path of the
// desired directory
File f = new File(new File(".").getCanonicalPath());
// Set the current directory
chooser.setCurrentDirectory(f);
} catch (IOException e) {
}