tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Commons IO > Filters > Magic Number Filter

Magic Number Filter 

Apache Commons IO is a java library with simple IO utilities and filters. This requires the library commons-io-2.1.jar to be in classpath. The following example shows using FileFilterUtils.magicNumberFileFilter() API. It returns a filter that returns TRUE if the file begins with the provided magic number.


File Name  :  
com/bethecoder/tutorials/commons_io/tests/filters/MagicNumberFilterTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.commons_io.tests.filters;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.filefilter.FileFilterUtils;

public class MagicNumberFilterTest {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] args) {
    File directory = new File("C:/input");
    
    //Filter PNG files      //?   P   N   G  
    byte [] pngMagicNum = -119807871 };
    String[] files = directory.list(
        FileFilterUtils.magicNumberFileFilter(pngMagicNum));
    
     for (String file : files) {
         System.out.println(file);
     }
     
     System.out.println();
     
     //Filter Bitmap files  //B  M
     byte [] bmpMagicNum = 6677 };
     files = directory.list(
          FileFilterUtils.magicNumberFileFilter(bmpMagicNum));
      
     for (String file : files) {
         System.out.println(file);
     }
  }
}
   

It gives the following output,
console.png
db.png

ascii_table.bmp



 
  


  
bl  br