Partager via


Display Raw Contents of a file

Sometimes you just want to see the raw contents of a file. This is especially useful when you don’t have any cool editor/program which can read that file and present the contents in some useful manner. Like you may want to see contents of a zipped file or contents of a file that contains serialized data of an object.

The following J# program reads the content of the file and outputs the same on the standard output console. All the bytes read are written in hex format. It also tells you the no of bytes read from the file.

import java.io.*;

public class file_bytes

{

      public static void main(String[] args) throws FileNotFoundException, IOException

      {

            FileInputStream fi = new FileInputStream(args[0]);

            int read_byte;

            int length = 0;

            while ((read_byte = fi.read()) != -1)

            {

                  System.out.print(Integer.toHexString(read_byte) + " ");

                  length++;

            }

            System.out.print("\n");

            System.out.print("Total no of bytes read :" + length);

      }

}

All you need to do is to compile this file using the J# compiler ( vjc.exe ) which would be usually present at the windir\Microsoft.NET\Framework\version where windir identifies your Windows directory and version is a version number identifying a specific version of the .NET Framework.

Then while executing the generated exe, give the complete path of the file whose contents you want to read.

For example, using notepad I created a file whose contents are simply :

Let us see what happens!

Suppose the file is stored as test.txt at C:\temp\

Now, if my exe created after compiling the above J# program is “file_bytes.exe”, then all I need to do is run my exe as :

file_bytes.exe "C:\\temp\\test.txt"

and I see the output as :

4c 65 74 20 75 73 20 73 65 65 20 77 68 61 74 20 68 61 70 70  65 6e 73 21

 

Total no of bytes read :24

Now, lets do something interesting here. I zipped the test.txt file using the WinZip and save it as test.zip at the same location.

Now if I run the my exe as :

file_bytes.exe "C:\\temp\\test.zip"

 

I see the output as :

50 4b 3 4 a 0 0 0 0 0 78 73 7c 34 ea 97 1 f7 18 0 0 0 18 0 0 0 8 0 0 0 74 65 73 74 2e 74 78 74 4c 65 74 20 75 73 20 73 65 65 20 77 68 61 74 20 68 61 70 70 65 6e 73 21 50 4b 1 2 14 0 a 0 0 0 0 0 78 73 7c 34 ea 97 1 f7 18 0 0 0 18 0 0 0 8 0 0 0 0 0 0 0 1 0 20 0 0 0 0 0 0 0 74 65 73 74 2e 74 78 74 50 4b 5 6 0 0 0 0 1 0 1 0 36 0 0 0 3e 0 0 0 0 0

Total no of bytes read :138

 

As you can see, this time contents of the file grew larger than what it was !

 

Check my next blog which would use this app to demonstrate Object Serialization.

Comments