Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Maybe not the answer you wanted, but as you gave us very little information ...</p> <p>In unixoid systems (Linux, Mac, *BSD) you have the <a href="http://linux.die.net/man/1/file" rel="nofollow">file</a> command, that</p> <blockquote> <p>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.</p> </blockquote> <p>E.g.</p> <pre><code>$ 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) </code></pre> <p>Using <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec%28java.lang.String%5b%5d,%20java.lang.String%5b%5d,%20java.io.File%29" rel="nofollow">Runtime.exec(...)</a> you could invoke that program and parse its output.</p> <h3>Edit 1:</h3> <p>To determine if a given file is a <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics" rel="nofollow">PNG</a>:</p> <pre><code>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 &lt; MAGIC.length; ++i) { if(ins.read() != MAGIC[i]) { return false; } } return true; } finally { ins.close(); } } } </code></pre> <h3>Edit 2:</h3> <p>Sometimes <a href="http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html#getContentType%28%29" rel="nofollow">URLConnection.getContentType()</a> works, too, for local files:</p> <pre><code>new File(name).toURI().toURL().openConnection().getContentType() </code></pre> <p>But your comments sound like you <em>have to</em> implement the method by yourself, not using external programs (?).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload