2013-12-08 33 views
1

我是新來的java,我得到了用戶的路徑,使用chooser.getCurrentDirectory(),現在我想用目錄在那裏創建一個文件,File report = new File(chooser directory + "filename"),但它只接受字符串,而不是文件,所以我怎樣才能得到選擇器目錄作爲一個字符串?目錄到字符串,Java

+1

請發佈您的代碼,你到目前爲止。 – skiwi

+0

試試{\t String choosertitle = null; \t pathchooser = new JFileChooser(); pathchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); \t \t \t \t \t pathchooser.showSaveDialog(null); \t \t \t \t \t \t File report = new File(pathchooser.getCurrentDirectory()+ filename +「calibration report」); – user2017463

回答

4

你不應該使用chooser.getCurrentDirectory()開始,你應該使用chooser.getSelectedFile();

你應該看看http://docs.oracle.com/javase/7/docs/api/java/io/File.html,特別是在涉及文件名的get*()方法。

+0

非常感謝你的工作。 – user2017463

+0

@ user2017463如果您認爲這是正確的答案,您能否將答案標記爲已接受?更多參考看看:http://stackoverflow.com/tour – skiwi

+0

實際上,有一個問題,現在當我選擇一個目錄像(C:\ Users \ me \ Documents),來保存文件(example.txt)它將文件以名稱(Documentsexample.txt)保存到(C:\ Users \ me) – user2017463