Here you can find the source of isValidDirectory(String name)
Parameter | Description |
---|---|
name | a parameter |
public static boolean isValidDirectory(String name)
//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; } } }