Reputation:
I am currently building an application, where I want a whole directory to be imported in another one. I am working with apache commons.io library that you need to import a s a jar file.
Let's say from a directory that I will choose with a JfileChooser from GuiBuilder Netbeans into C:\output. I want all the images to be imported there. My code is not running. When I am pressing JButton2 ( the button responsible for loading a directory I get an exception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at WebAppImg.jButton2ActionPerformed(WebAppImg.java:141) ( The second line points out to : String filename = f.getAbsolutePath(); )
copyDirectoryToDirectory method maybe. But my thoughts on doing that was that when putting FileSelectionMode to be Directories_Only that the absolute path would be something like C:\importFile and not C:\importFile\image1.jpg for example so it would not be wrong. But maybe this is the mistake?
So far, this is my code.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:(for importing a whole directory(folder) from C:\images1 into C:\output ).
JFileChooser chooser = new JFileChooser();
chooser = new JFileChooser();
//chooser.setCurrentDirectory(new java.io.File("."));
//chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//chooser.showOpenDialog(null);
//chooser.setAcceptAllFileFilterUsed(false);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
try {
File srcDir = new File(filename);
File destDir = new File("C:/output/");
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 1
Views: 1035
Reputation: 11968
Try this.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String SourceDir = f.getAbsolutePath();
try {
File srcDir = new File(SourceDir);
File destDir = new File("C:/output/");
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 0
Reputation: 1113
I believe that you are not using the correct FileUtils
method. Please try using copyDirectory(File srcDir, File destDir);
UPDATE
If you want to copy all the files in the src directory use the method above. If you want to copy the directory let's say C:\images
to C:\COPIED
the copyDirectoryToDirectory(File srcDir, File destDir);
will result in C:\COPIED\images
since it copies the whole directory into another directory.
Here is my working example...
private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {
File src = jFileChooser1.getSelectedFile();
File dest = new File("C:\\COPIED");
System.out.println(src.getAbsolutePath());
System.out.println(dest.getAbsolutePath());
try {
FileUtils.copyDirectory(src, dest);
//FileUtils.copyDirectoryToDirectory(src, dest);
} catch (IOException ex) {
Logger.getLogger(FileChoosingFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here is a working example: stackoverflow-jchooser.zip It should only be used as a starting point. The copying should be done in a separate thread instead of the EDT.
Upvotes: 1