以前にファイルに書き込んだパスにディレクトリを設定したいだけです。
したがって、私は使用しました:
fileChooser.setCurrentDirectory(new File("path.txt"));
そしてpath.txtでパスが与えられます。しかし、残念ながらこれはうまくいきません。なぜ:Pなのか疑問に思います。私はそれをすべて間違っていたと思いsetCurrentDic
ます..
以前にファイルに書き込んだパスにディレクトリを設定したいだけです。
したがって、私は使用しました:
fileChooser.setCurrentDirectory(new File("path.txt"));
そしてpath.txtでパスが与えられます。しかし、残念ながらこれはうまくいきません。なぜ:Pなのか疑問に思います。私はそれをすべて間違っていたと思いsetCurrentDic
ます..
setCurrentDirectory
ディレクトリを表すファイルをパラメータとして受け取ります。パスが書かれたテキストファイルではありません。
必要なことを行うには、ファイル「path.txt」を読み取り、読み取った内容で File オブジェクトを作成し、このファイルを setCurrentDirectory に渡す必要があります。
String pathWrittenInTextFile = readFileAsString(new File("path.txt"));
File theDirectory = new File(pathWrittenInTextFile);
fileChooser.setCurrentDirectory(theDirectory);
の内容を読む必要がありpath.txt
ます。最も簡単な方法はcommons-ioを使用することです:
String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File dir = new File(fileContents);
使用することもできます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) {
}