0

I have a little problem, I need to do a program that open the file explorer and copy the selected file in a specified directory. I've only found how to open the explorer :

 File file = new File ("c:\\<directory>");
 Desktop desktop = Desktop.getDesktop();
 desktop.open(file);

but I need to get the selected file and copy in a default directory, and I really don't know how to do it. Thanks a lot!!

Pasq
  • 45
  • 8

2 Answers2

0

Try read the file and write it to another directory. You can use FileReader() and FileWriter() methods.

Bhoke
  • 468
  • 6
  • 22
0

I assume you have a SWT Java Application. Then you can use FileDialog to show a file chooser where the user selects a file. Without a GUI it wouldn't be easy to show a file choosing dialog to the user. This code snippet uses FileUtils from Apache Commons IO:

FileDialog dlg = new FileDialog(frame, "Choose a file to copy", FileDialog.OPEN);
dlg.setVisible(true); // blocks until user completes the action
String fileToCopyString = dlg.getFile();

if (fileToCopyString != null) {
  File fileToCopy = new File(fileToCopyString);

  if (fileToCopy.isFile()) {
    FileUtils.copyFile(fileToCopy, new File(tmpDir, fileToCopy.getName());
  }
}

References:

Community
  • 1
  • 1
nif
  • 3,342
  • 20
  • 18