Java Is Directory Valid isValidDirectory(String name)

Here you can find the source of isValidDirectory(String name)

Description

Checks whether a given directory contains valid images for classification

License

Open Source License

Parameter

Parameter Description
name a parameter

Return

true if valid; false otherwise

Declaration

public static boolean isValidDirectory(String name) 

Method Source Code

//package com.java2s;

import java.io.File;

import javax.activation.MimetypesFileTypeMap;

public class Main {
    /**/* w  w  w .j av a  2 s.  c om*/
     * Checks whether a given directory contains valid images for classification
     * @param name
     * @return true if valid; false otherwise
     */
    public static boolean isValidDirectory(String name) {
        File f = new File(name);

        for (String file : f.list()) {
            if (!isImage(file, false)) {
                System.out.println("Error: Directory contains non-image file:\n" + file.toString());
                return false;
            }
        }

        return true;

    }

    /**
     * Obtains a files MIME type and returns true if it is of type image
     * @param fileName
     * @param output
     * @return true if an image; false otherwise
     */
    public static boolean isImage(String fileName, boolean output) {

        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String mimeType = mimeTypesMap.getContentType(fileName);

        if (output) {
            System.out.println("Validating input file...");
            System.out.println("File type: " + mimeType);
        }

        if (mimeType.startsWith("image")) {
            return true;
        } else {

            if (output) {
                System.out.println("Invalid file type input. Please enter an image");
            }

            return false;
        }

    }
}

Related

  1. isValidDirectory(File directory)
  2. isValidDirectory(File directory)
  3. isValidDirectory(File f)
  4. isValidDirectory(final File folder)
  5. isValidDirectory(String filePath)
  6. isValidDirectory(String path)