Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating a binary dump of a file

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a binary dump of a file

    Hi all,

    I'm trying to get back into Java again after a decent period away. I'm currently working on creating a very basic virus searching program. For this to work I need to create a class that is able to take an input stream from a given file and convert this to its binary representation. I am then going to shift it to Hex to make it more readable. This can then be used to scan for any virus signatures I have in my database. I've been experimenting with the different input streams Java provides but am having difficulties figuring out which one would be best.

    If anyone can provide me with a pointer in the right direction for which inbuilt Java input streams I should be using or if there is any example code for this.

    Thanks,

    Neil.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a binary dump of a file

    take an input stream from a given file and convert this to its binary representation
    You can read any file as bytes into a byte array. No need to convert it. It IS binary.
    The Integer.toHexString() could be used to convert the bytes to a String hex representation. The problem with the method is that it drops leading 0s.
    Or write your own simple converter using a String array with letters 0-F and index it with the nibbles of the byte.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating a binary dump of a file

    Most streams should work perfectly fine. For this kind of project, a FileReader wrapped in a BufferedReader should work quite nicely.

    You can read in the file one byte at a time and then display the hex using the method Norm described (the underlying storage of the numbers is all the same, it's just how you display it).

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a binary dump of a file

    You might not want to use a Reader. Readers are for character input. You want byte input.
    The FileInputStream would read bytes. From the API doc:
    FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a binary dump of a file

    Hey,

    This gives me a lot of good starting points, will give them all a try and update on the progress.

    The help is really appriciated.

    Neil.

Similar Threads

  1. Please help me analyze this java core dump on AIX
    By acejun01 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 12th, 2014, 05:21 AM
  2. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  3. Problem in Creating Jar file
    By sikriyogesh in forum Member Introductions
    Replies: 0
    Last Post: November 20th, 2009, 02:12 AM
  4. Creating a class file
    By ipatch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2009, 07:19 PM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM