Advertisement
Guest User

Untitled

a guest
Feb 27th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. package replaceHeader;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7.  
  8. public class replaceHeader {
  9.    
  10.     static String hexString = ""; // A hex string that you want to use as replacement for the first 512 bits. 512 bits in hex is 1024 characters
  11.     static byte[] headerBits = hexStringToByteArray(hexString);
  12.     static File fil;
  13.    
  14.     static String path = "/path/to/folder/containing/files"; // EDIT THIS!!  
  15.    
  16.     public static void main(String[] args) throws IOException{
  17.        
  18.  
  19.        
  20.         File folder = new File(path);
  21.         File[] listOfFiles = folder.listFiles();
  22.        
  23.         for(int i = 0; i < listOfFiles.length; i++){
  24.             fil = new File(listOfFiles[i]+"");
  25.             replaceBytes();
  26.         }
  27.        
  28.        
  29.        
  30.  
  31.     }
  32.     public static byte[] hexStringToByteArray(String s) {
  33.         int len = s.length();
  34.         byte[] data = new byte[len / 2];
  35.         for (int i = 0; i < len; i += 2) {
  36.             data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
  37.                                  + Character.digit(s.charAt(i+1), 16));
  38.         }
  39.         return data;
  40.     }
  41.     public static void replaceBytes() throws IOException{
  42.         RandomAccessFile raf = new RandomAccessFile(fil, "rw");
  43.         try {
  44.             raf.seek(0);
  45.             raf.write(headerBits, 0, hexString.length()/2);
  46.         } finally {
  47.             raf.close();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement