JFileChooser 允许访问特定文件夹并将文件复制到其中。

huangapple go评论130阅读模式
英文:

JFileChooser give access to a specific folder and copy a file to it

问题

I want to upload a file into a local folder (on my PC). The user should select a file. Than this file should be loaded into a folder (this folder is hard coded in my Java class). Unfortunately, I didn't find a good solution for this. I just found something about a JFileChooser which is nice to open a file, but not to load one.

public static void main(String[] args){
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "JPG & GIF Images", "jpg", "gif");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " +
                chooser.getSelectedFile().getName());
    }
}

Is it possible to just give the file chooser access to a specific folder? The folder is hard coded like this:

public static String uploadPath = System.getProperty("user.dir") + "/uploads Modul1";
英文:

I want to upload a file into a local folder (on my PC). The user should select a file. Than this file should be loaded into a folder (this folder is hard coded in my Java class). Unfortunately I didn't find a good solution for this.
I just found something about a JFileChooser which is nice to open a file, but not to load one.

public static void main(String[] args){
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "JPG & GIF Images", "jpg", "gif");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " +
                chooser.getSelectedFile().getName());
    }
}

Is it possible to just give the file chooser access to a specific folder? the folder is hard coded like this:

public static String uploadPath = System.getProperty("user.dir") + "/uploads Modul1";

答案1

得分: 1

复制或移动文件与文件的选择方式无关。复制文件的简单方法是使用Files.copy方法。以下是如何将其添加到您的现有代码中的示例:

int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
    File selectedFile = chooser.getSelectedFile();
    System.out.println("You chose to copy this file: " +
        selectedFile.getName());

    Path newPath = Paths.get(uploadPath, selectedFile.getName());
    Files.copy(selectedFile.toPath(), newPath);
}

官方教程有关复制文件的部分:https://docs.oracle.com/javase/tutorial/essential/io/copy.html

如果您想要移动文件而不是复制文件,请使用Files.move而不是Files.copy

英文:

Copying or moving files is independent of how the file is chosen. The easy way to copy a file is with the Files.copy method. Here's how you can add it to your existing code:

    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        File selectedFile = chooser.getSelectedFile();
        System.out.println("You chose to copy this file: " +
            selectedFile.getName());

        Path newPath = Paths.get(uploadPath, selectedFile.getName());
        Files.copy(selectedFile.toPath(), newPath);
    }

The official tutorial has a section on copying files: https://docs.oracle.com/javase/tutorial/essential/io/copy.html

If you want to move the file instead of copying it, use Files.move instead of Files.copy.

huangapple
  • 本文由 发表于 2020年8月12日 19:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63376016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定