簡體   English   中英

復制從jfilechooser選擇的文件

[英]Copying file selected from jfilechooser

我正在使用文件選擇器來選擇圖像文件。 單擊打開按鈕后,我想將圖像復制到我的項目文件夾中。代碼沒有顯示任何錯誤,但沒有任何反應。 什么也不會復制。

 if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        File target = new File("/"+file.getName());
        try {
            Files.copy(file.toPath(),target.toPath(),REPLACE_EXISTING);
        } catch (IOException ex) {
            Logger.getLogger(detail1.class.getName()).log(Level.SEVERE, null, ex);
        }

}

我在File target = new File("/"+file.getName());使用了','而不是'+' File target = new File("/"+file.getName()); 但沒有運氣,這是什么問題?

這解決了我的問題:
File target = new File(System.getProperty("user.dir")+"/images",file.getName());

從Java 7開始,您可以像這樣使用Files.copy

Files.copy(Paths.get(System.getProperty("user.dir"), "images"), file.toPath());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM