Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6827007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:12:55+00:00 2026-05-26T22:12:55+00:00

Does any one know how to read the header of a file in java

  • 0

Does any one know how to read the header of a file in java using “magic numbers” or ascii values to get the name of the extension of a file

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:12:55+00:00Added an answer on May 26, 2026 at 10:12 pm

    Maybe not the answer you wanted, but as you gave us very little information …

    In unixoid systems (Linux, Mac, *BSD) you have the file command, that

    tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests. The first test that succeeds causes the file type to be printed.

    E.g.

    $ file linux-image-3.1.0-030100rc10-generic_3.1.0-030100rc10.201110200610_amd64.deb
    linux-image-3.1.0-030100rc10-generic_3.1.0-030100rc10.201110200610_amd64.deb: Debian binary package (format 2.0)
    

    Using Runtime.exec(…) you could invoke that program and parse its output.

    Edit 1:

    To determine if a given file is a PNG:

    import java.io.*;
    
    public class IsPng {
    
        public static void main(String ...filenames) throws Exception {
            if(filenames.length == 0) {
                System.err.println("Please supply filenames.");
                return;
            }
    
            for(String filename : filenames) {
                if(isPng(new File(filename))) {
                    System.out.println(filename + " is a png.");
                } else {
                    System.out.println(filename + " is _not_ a png.");
                }
            }
        }
    
        private static final int MAGIC[] = new int[] { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
    
        private static boolean isPng(File filename) throws Exception {
            FileInputStream ins = new FileInputStream(filename);
            try {
                for(int i = 0; i < MAGIC.length; ++i) {
                    if(ins.read() != MAGIC[i]) {
                        return false;
                    }
                }
                return true;
            } finally {
                ins.close();
            }
        }
    
    }
    

    Edit 2:

    Sometimes URLConnection.getContentType() works, too, for local files:

    new File(name).toURI().toURL().openConnection().getContentType()
    

    But your comments sound like you have to implement the method by yourself, not using external programs (?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does any one know how to get the current motherboard, processor or HD temperature
Does anyone know how to read a x.properties file in Maven. I know there
Does any one know how can I convert a BSTR to an int in
Does any one know of a way to control how Visual Studio handles curly
Does any one know of a control that i can use with a ASP.Net
Does anyone know a good C++ class to read (and possibly also write) INI
Does anyone know of a good class to read in .ged files Gedcom is
Does anyone know when calling 'seek' and 'read' , how is the hard-drive physicly
Does anyone know of an existing ruby implementation of a read/write lock - http://en.wikipedia.org/wiki/Readers-writer_lock
Does anyone know what is required to be able to read and write to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.