0

我只想将目录设置为我之前写在文件中的路径。

因此我使用了:

fileChooser.setCurrentDirectory(new File("path.txt"));

在 path.txt 中给出了路径。但不幸的是,这行不通,我想知道为什么:P。我想我完全错了setCurrentDic..

4

3 回答 3

2

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);
于 2011-02-16T12:38:09.257 回答
1

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(..)

于 2011-02-16T12:37:43.760 回答
0
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) {
}
于 2011-02-16T12:37:09.397 回答