簡體   English   中英

如何 <move selected jpeg image file> 借助JFileChooser到Java swing中的選定文件夾?

[英]How to <move selected jpeg image file> with the help of JFileChooser to a selected folder in java swing?

在java swong中,使用JFileChooser,我想選擇一個圖像並將所選圖像移動到所需的文件夾。

但不知道如何移動?


更新 :這就是我所做的

public void actionPerformed(ActionEvent evt) {
    imageFileChooser.setVisible(true); 
    int checkIfOpened = imageFileChooser.showOpenDialog(this); 
    if (checkIfOpened == JFileChooser.APPROVE_OPTION) { 
        File file = imageFileChooser.getSelectedFile(); 
        // int size = file.getLength(); 
        // don't know what to do here
    }else{ 
    } 
}

JFileChooser有一個getSelectedFile()方法,使用它,然后用該文件打開一個FileInputStream。 然后創建具有所需目標的FileOutputStream。 然后,您可以獲得FileChannels並使用transferTo()方法:

int size = file.getLength();
fileInputStream.getChannel().transferTo(0, size, fileOutputStream.getChannel());
fileOutputStream.close();
file.delete();

使用File#renameTo()

File file = imageFileChooser.getSelectedFile(); 
File destination = new File("/path/to/new/location", file.getName());
boolean success = file.renameTo(destination);
// You might want to check success result here.

暫無
暫無

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

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