如何<移动选定的jpeg图像文件>借助JFileChooser到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{ 
    } 
}
    
已邀请:
使用
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.
    
JFileChooser有一个getSelectedFile()方法,使用它,然后用该文件打开一个FileInputStream。然后创建具有所需目标的FileOutputStream。然后你可以获得FileChannels并使用transferTo()方法:
int size = file.getLength();
fileInputStream.getChannel().transferTo(0, size, fileOutputStream.getChannel());
fileOutputStream.close();
file.delete();
    

要回复问题请先登录注册