2

This post is a part 2 post of another one enquiry I made just days ago. Then with this code I will put last and through JButton1 I was able to attach a file and make it visible in a window north and east to the application. I am trying now to import a :

1) new image into a certain directory, for example into C:\output

2)a whole directory(folder) of images, lets say from C:\importImages

into C:\output .

For this I assume that I have some images in C:\importImages directory. Below , these are the 2 code samples that I need to fill in in order this thing that I want to work. This first method trying to load directories fails to run. I think that maybe its fault has to do something with Filechooser button from GUI Builder.

This is the whole method for loading a whole directory.

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:(for importing a whole directory(folder) from C:\importImages into C:\output ).
JFileChooser chooser = new JFileChooser();
        chooser = new JFileChooser(); 

        File f = chooser.getSelectedFile();

        String filename = f.getAbsolutePath();
    try {
        ImageIcon ii=new ImageIcon(scaleImage(250, 250, ImageIO.read(new File(filename))));//get the image from file chooser (directories)
        //jLabel1.setIcon(ii);

        File srcDir = new File(filename);
        File destDir = new File("C:/output/");
        FileUtils.copyDirectoryToDirectory(srcDir, destDir);

    }
        catch (Exception ex) {
        ex.printStackTrace();
    }
    }

and method for importing just a single file.

  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here(for importing a single image to directory C:\output ).
--
    }

Thanks in advance!

  • Please do not change your question in this way. Visitors can not know which of the original information, the answer relates. Please change the "question" back to their original state. And post a new question. – moskito-x Jan 07 '13 at 05:37
  • 1.) You can not pass a directory to the function scaleImage() it's impossible to scale a directory !!! `chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);` and so your code will not come to the next error. 2.) If you change JFileChooser to point to a file. `File srcDir = new File(filename);` Is not what you want! Because filename contains the whole path like C:/choosenDir/myImageFile.jpg and that's not allowed in the function "copyDirectoryToDirectory" `if (srcDir.exists() && srcDir.isDirectory() == false) {throw new IllegalArgumentException("Source '" "' is not a directory")` – moskito-x Jan 07 '13 at 06:05
  • You are right Mosquito. I will post another question in a bit about directory only. I changed the question back to its normal state. Please anwser that question that I will make if I can. Thanks for your help in this one. –  Jan 07 '13 at 12:28
  • Although you were right about the edit, I cant get it to work because of the error you re saying there is. You are right in that part. But I believe that in copyDirectoryToDirectory method the whole path will be C:/choosenDir/ and not the one you state, because i ve made it DIRECTORY.ONLY. I made a new post as you suggest about this one. –  Jan 07 '13 at 14:03
  • That what I meant you have to decide for one of the two options. If you code `chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);` you can not call `scaleImage()` and vice versa if you choose a file you can not call `copyDirectoryToDirectory()` . Tip use 2 variables 1.) `String filename = f.getAbsolutePath()` and 2.) `File srcDir = new File(f.getParent())` – moskito-x Jan 07 '13 at 16:32

1 Answers1

0

The best way to learn something, try it yourself.
I can show you the possibilities. Try these ways useful to insert in your code.

Question : I am trying now to import a :

  • 1) new image into a certain directory, for example into C:\output

Answer:

How to copy file in Java from one directory to another is common requirement, given that there is no direct method in File API for copying files from one location to another.

One way of copying file is reading from FileInputStream and writing same data to FileOutputStream to another directory.

Thankfully you don't need to reinvent wheel here, there are some open source library available which allows us to copy file in Java easily from one directory to another. One of such library is Apache commons IO which contains a class called FileUtils, which provides utility method for file related operation.

Copy and rename file on different location

FileUtils.copyFile

FileUtils.copyFile(sourceFile, targetFile) can be used

    String source = "C:/output/myImage.jpg";
    //directory where file will be copied
    String target ="C:/importImages/";

    //name of source file
    File sourceFile = new File(source);
    String Filename = sourceFile.getName();

    File targetFile = new File(target+Filename);

    //copy file from one location to other
    FileUtils.copyFile(sourceFile, targetFile);

Question : I am trying now to import a :

  • 2)a whole directory(folder) of images, lets say from C:\importImages into C:\output .

Answer:

What is the standard way nowadays to accomplish directory iteration with java?

Best way to iterate through a directory in java?

Add these ways in your code. Is not that hard.

  • Test for the file extension (.jpg .gif ...)
  • Don't forget to catch
  • NullPointerException - if source or destination is null
  • IOException - if source or destination is invalid
  • IOException - if an IO error occurs during copying
Community
  • 1
  • 1
moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • Thanks moskito for your great help! But there are some issues that I am still experiencing, I edited the post to reflect this! –  Jan 06 '13 at 23:15