英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论