zondag 22 juli 2012

Howto create a set of files on the fly (after the folders)


Intro
In this blog I would like to show what is the easiest way in java to create a set of files after we create the folders see my previous post howto create set of folders on the fly.

The problem.

We determined the folders we want to rename and fill them up with files. The first part I described this part is al logical follow up.

The solution

The filter we are going to use at this point, is at file level The filter would look something like this:

package com.hanichi.webshop.controller.image.resize;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;

public class ImageFilter implements FileFilter {
private final static String FILE_NAME_POST_FIX = "png";

@Override
public boolean accept(File pathname) {

return pathname.getAbsolutePath().endsWith(FILE_NAME_POST_FIX);
}

}

for (File folder : imagesFolders) {
   ImageFilter imageFilter = new ImageFilter();
File[] images = new File(folder.getAbsolutePath()).listFiles(imageFilter);
   String path = image.getAbsolutePath();
for (File image : images) {
     if(image.isFile()){
       String filePath =path.replace(IMAGES, ICONS); 
      }
}
}
}

This is a very fast way to code in short time how to copy and rename files.

Have fun!

vrijdag 20 juli 2012

Howto create a set of folders on the fly

Intro
In this blog I would like to show what is the easiest way in java to create a set of folders.
I took for this example the folder structure of images and created the icon folders for them (lazy as I am :-) )

The problem
The structure of my images folders would be:

images/image_galery_1
images/image_galery_2
images/image_galery_3
images/image_galery_4.

In the past I would loop through the forrest of folder to find these four. There is a much easier way and copy them with other names:


The solution
        FilenameFilter imageFilter = new ImageFilter();
        File[] imagesFolders = new File("images",.listFiles(imageFilter);

java.io.FileNameFilter is a standard java interface that helps you to identify the folders you looking for. Create a class yourself that implements this Interface. The class would look something like this:

public class ImageFilter implements FilenameFilter {
    private static final String FOLDER_PRE_FIX="image";

    @Override
    public boolean accept(File dir, String name) {
        boolean fileImage=false;
        if(name.startsWith(FOLDER_PRE_FIX)){
            fileImage=true;
        }
        return fileImage;
    }

In this example I just used a simple 2 layered folder structure. You can imagine that in a more complex structure this way realy is helpfull.

After running this loop you automated the creation of the icon folders with minimized lines of code :
    private static final String ICONS = "icons";
    private static final String IMAGES = "images";

Looping through the results  the following way:
        for (File file : imagesFolders) {
           //Just to be sure nothing slips through.
            if(file.isDirectory()){
                String filePath = file.getAbsolutePath().replace(IMAGES, ICONS);
                File iconsFolder = new File(filePath);
                iconsFolder.mkdirs();
            }

have fun!