User:Simpsons contributor/Produce orderly image directory

package filecopier;

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException;

public class OrderlyDirectory {   /**     * Takes all known images in a directory and rewrites them in a      * more orderly form: 1.png, 2.png, etc. Image extension and type * remains intact * @param directory Path of directory to be ordered * @throws java.io.FileNotFoundException * @throws java.io.IOException */   public void order(String directory) throws FileNotFoundException, IOException {       FileHandling handle = new FileHandling; //Check if directory points to a directory File directoryTest = new File(directory); if(directoryTest.isDirectory == false) throw new FileNotFoundException(directory + " does not point to a directory"); //Create a temporary directory to store all images boolean exists = true; String randomName = "C:/" + randomName(100) + "/"; File randomDirectory = new File(randomName); //Check if that directory already exists do       { if(randomDirectory.isDirectory == true) {               randomName = randomName = "C:/" + randomName(100) + "/"; randomDirectory = new File(randomName); }//End of random rename else exists = false; } while (exists == true); //Create that temporary directory randomDirectory.mkdir; //Copy all image files from randomName to diretory String [] fileNames = directoryTest.list; int counter = 0; for(String fileName : fileNames) {           File fileTest = new File(directory + fileName); if(fileTest.isFile == true && isImage(fileName) == true) {               String type = getType(fileName); handle.copyFile(directory + fileName, randomName + counter +                               "." + type, true); counter++; } //End of copy } //End of file transfer for //Empty the original directory handle.emptyDirectory(directory); /*Transfer all copied files from the random directory back to the * original directory in their new orderly form. */       String directoryName = "C:/" + randomDirectory.getName + "/"; fileNames = randomDirectory.list; for(String fileName : fileNames) handle.copyFile(directoryName + fileName, directory + fileName, true); //Delete temporary directory handle.deleteDirectory(directoryName); }   /**     * Produces a random String * @param length Number of elements in the string * @return A random string with (length) elements */   private String randomName(int length) {       String name = new String; for(int i=0; i<=length; i++) {           int randomNumber = (int) (10 * Math.random); name += randomNumber; } //End of random generate return name; }   /**     * Checks a file name to see if it contains a known * image file extension and is therefore an image file. * @param fileName File name with or without path. Extension must * be included. * @return True if the tested fileName contains a known image file * extension, false if it doesn't.    */ private boolean isImage(String fileName) {       boolean image = false; //See if fileName contains known image extensions //Portable Network Graphics if(fileName.contains(".png")) image = true; if(fileName.contains(".PNG")) image = true; //Joint Photographic Experts Group if(fileName.contains(".jpg")) image = true; if(fileName.contains(".JPG")) image = true; if(fileName.contains(".jpeg")) image = true; if(fileName.contains(".JPEG")) image = true; //Graphics Interchange Format if(fileName.contains(".gif")) image = true; if(fileName.contains(".GIF")) image = true; //Uncompressed bitmap if(fileName.contains(".bmp")) image = true; if(fileName.contains(".BMP")) image = true; return image; }   /**     * Return the image extension from a file name with or without * path, eg "C:/Image.jpg" returns "jpg" "Image.png" returns png. * @param fileName File name with or without path. Must contain * extension. * @return Image type extension. */   private String getType(String fileName) {       String imageType = new String; //See if fileName contains known image extensions //Portable Network Graphics if(fileName.contains(".png")) return "png"; if(fileName.contains(".PNG")) return "png"; //Joint Photographic Experts Group if(fileName.contains(".jpg")) return "jpg"; if(fileName.contains(".JPG")) return "jpg"; if(fileName.contains(".jpeg")) return "jpg"; if(fileName.contains(".JPEG")) return "jpg"; //Graphics Interchange Format if(fileName.contains(".gif")) return "gif"; if(fileName.contains(".GIF")) return "gif"; //Uncompressed bitmap if(fileName.contains(".bmp")) return "bmp"; if(fileName.contains(".BMP")) return "bmp"; return imageType; } }