简体   繁体   English

NetBeans FileChooser。 如何使用它将文件从一个位置复制到字符串中定义的另一个位置?

[英]NetBeans FileChooser. How do I use it to copy a file from one location to another defined in a string?

I know how to perform simple operations with FileChooser within netbeans but when it comes to more complex ones, I'm getting a little stuck. 我知道如何在netbeans中使用FileChooser执行简单的操作,但是当涉及到更复杂的操作时,我会陷入困境。 So far I'm able to make use of a picture attained from the user's file system on the JFrame as below: But I don't know how to use it to copy a file from one location to another defined in a string? 到目前为止,我能够使用从JFrame上的用户文件系统获得的图片,如下所示:但我不知道如何使用它将文件从一个位置复制到字符串中定义的另一个位置?

code: 码:

private void ImageAtattchActionPerformed(java.awt.event.ActionEvent evt) {
    int o = jFileChooser1.showOpenDialog(this);
    if (o == JFileChooser.APPROVE_OPTION) {
        File f = jFileChooser1.getSelectedFile();
        jLabel1.setIcon(new ImageIcon("" + f));
    }

"But I don't know how to use it to copy a file from one location to another defined in a string?" “但我不知道如何使用它将文件从一个位置复制到字符串中定义的另一个位置?”

Assuming you trying to save an image file, just use ImageIO.read and ImageIO.write 假设您尝试保存图像文件,只需使用ImageIO.readImageIO.write

  1. Read the image you get from the file 阅读从文件中获取的图像

      image = ImageIO.read(file); 
  2. Write it to a different file 将其写入不同的文件

     ImageIO.write(image, "jpg",new File("C:\\\\path\\\\to\\\\destination\\\\" + file.getName())); 

See more at ImageIO api . ImageIO api上查看更多信息。 Also see Basic I/O for saving other formats, besides just images. 另请参阅基本I / O以保存其他格式,除了图像。


You can run this example, just change the file path. 您可以运行此示例,只需更改文件路径即可。

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

public class SaveImageFile {

    public SaveImageFile() {
        final JFrame frame = new JFrame("Save Image");

        JButton saveImage = new JButton("Browse");
        saveImage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "JPG, GIF, and PNG Images", "jpg", "gif", "png");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(frame);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    System.out.println("You chose to open this file: "
                            + file.getName());
                    BufferedImage image;
                    try {
                        image = ImageIO.read(file);
                        ImageIO.write(image, "jpg",new File("C:\\path\\to\\destination\\" + file.getName()));
                    } catch (IOException ex) {
                        Logger.getLogger(SaveImageFile.class.getName()).log(Level.SEVERE, null, ex);
                    }              
                }
            }
        });
        frame.add(saveImage);
        frame.setLayout(new GridBagLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SaveImageFile saveImageFile = new SaveImageFile();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM