com.drew.imaging.FileTypeDetector Java Examples

The following examples show how to use com.drew.imaging.FileTypeDetector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProcessAllImagesInFolderUtility.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@NotNull
private static PrintWriter openWriter(@NotNull File file) throws IOException
{
    // Create the output directory if it doesn't exist
    File metadataDir = new File(String.format("%s/metadata", file.getParent()));
    if (!metadataDir.exists())
        metadataDir.mkdir();

    File javaDir = new File(String.format("%s/metadata/java", file.getParent()));
    if (!javaDir.exists())
        javaDir.mkdir();

    String outputPath = String.format("%s/metadata/java/%s.txt", file.getParent(), file.getName());
    Writer writer = new OutputStreamWriter(
        new FileOutputStream(outputPath),
        "UTF-8"
    );
    writer.write("FILE: " + file.getName() + NEW_LINE);

    // Detect file type
    BufferedInputStream stream = null;
    try {
        stream = new BufferedInputStream(new FileInputStream(file));
        FileType fileType = FileTypeDetector.detectFileType(stream);
        writer.write(String.format("TYPE: %s" + NEW_LINE, fileType.toString().toUpperCase()));
        writer.write(NEW_LINE);
    } finally {
        if (stream != null) {
            stream.close();
        }
    }

    return new PrintWriter(writer);
}