Figure out the file format without extension

Asked

Viewed 808 times

2

I have thousands of photos on my computer but without the extension.

As the extension has been deleted, I would like some Java library to manipulate those files/photos that are without extension and then discover their format to rename. As there are thousands of photos, checking one by one will take a long time. And as I like Java, it will be a learning too.

2 answers

0

You can call the Trid through a system command inside Java. It already has the option to already rename the files to you by adding the extension.

Note that Trid has an executable for each operating system, so you’ll have to have a logic for each system you support.

0

You can do yes take the file without extension and convert to one with the desired extension.

public static void main(String[] args) throws IOException {

        String path = "C:\\Files\\Foto";

        byte[] imageInByte;
        BufferedImage originalImage = ImageIO.read(new File(
                path));


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String contentType = new Tika().detect(baos.toByteArray());
        ImageIO.write(originalImage, contentType, baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        baos.close();


        InputStream in = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);

        ImageIO.write(bImageFromConvert, contentType, new File(
                "C:\\Files\\Foto));

    }

Use that lib to detect the file type: https://www.apache.org/dyn/closer.cgi, lower and put on your classpath.

  • Let’s say my file name is in "C: Photo Files", where photo is without its extension so it generates an Exception saying that the file does not exist.

  • Qual Exception ?

  • I TESTED IT RIGHT HERE!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.