KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HexWriter


1 // HexWriter.java
2

3 import java.io.*;
4
5 public class HexWriter {
6
7     public static void main( String JavaDoc [] args )
8     {
9     if (args.length != 1 ) {
10         System.err.println( "USAGE: java HexWriter <FILENAME>" );
11         System.exit(1);
12     }
13
14     String JavaDoc filename = args[0];
15     int column_width = 10;
16     int retval = 0;
17     
18     System.out.println( " // Java hex dump byte data from file:" + filename );
19
20     FileInputStream fis = null;
21     BufferedInputStream bis = null;
22
23     try {
24         fis = new FileInputStream( filename );
25         bis = new BufferedInputStream( fis );
26
27         // Read in one line , the cipher text, from the
28
// encrypted file.
29
int k, q, len;
30         byte[] buffer = new byte[1024]; // 8K buffer
31
q=0;
32
33         String JavaDoc newline = System.getProperty( "line.separator");
34         System.out.print( " byte data[] = {" );
35
36         for (;;) {
37         len = bis.read( buffer, 0, buffer.length );
38         
39         if ( len < 0 ) break; // EOF
40

41         for (k=0; k<len; ++k, ++q) {
42             if ( q % column_width == 0) {
43             System.out.print( newline + "\t" );
44             }
45             System.out.print( "(byte)" );
46             System.out.print( "0x"+Integer.toHexString( buffer[q] & 0xFF ) + ", ");
47         }
48         }
49         System.out.println( newline + " };" );
50     }
51     catch ( IOException ioe ) {
52         // Error occurred reading license
53
retval = 69;
54         System.err.println( "I/O Exception :" + ioe.getMessage() );
55     }
56     finally {
57         if (bis != null) {
58         // Close the buffered input stream.
59
try { bis.close(); } catch (IOException ioe) { ; }
60         }
61         if (fis != null) {
62         // Close the file input stream.
63
try { fis.close(); } catch (IOException ioe) { ; }
64         }
65     }
66
67     System.exit( retval );
68     }
69 }
70
71 // fini
72
Popular Tags