0

以前にファイルに書き込んだパスにディレクトリを設定したいだけです。

したがって、私は使用しました:

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

そしてpath.txtでパスが与えられます。しかし、残念ながらこれはうまくいきません。なぜ:Pなのか疑問に思います。私はそれをすべて間違っていたと思いsetCurrentDicます..

4

3 に答える 3

2

setCurrentDirectoryディレクトリを表すファイルをパラメータとして受け取ります。パスが書かれたテキストファイルではありません。

必要なことを行うには、ファイル「path.txt」を読み取り、読み取った内容で File オブジェクトを作成し、このファイルを setCurrentDirectory に渡す必要があります。

String pathWrittenInTextFile = readFileAsString(new File("path.txt"));
File theDirectory = new File(pathWrittenInTextFile);
fileChooser.setCurrentDirectory(theDirectory);
于 2011-02-16T12:38:09.257 に答える
1

の内容を読む必要がありpath.txtます。最も簡単な方法はcommons-ioを使用することです:

String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File dir = new File(fileContents);

使用することもできます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 に答える