aspose file tools*
The moose likes Beginning Java and the fly likes Java: Convert a binary file to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java: Convert a binary file to "text" and back again." Watch "Java: Convert a binary file to "text" and back again." New topic
Author

Java: Convert a binary file to "text" and back again.

Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Hello

I am fooling aroung with Java, wondering how to read a binary file and convert it to text and then back again to binary.

This is what i have so far:


The above code does not work on binary data.

Kind regards Mads Nielsen
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Java has two kinds of classes for I/O:

  • Streams (InputStreams and OutputStreams) are for reading and writing binary data.
  • Readers and Writers are for reading files that can be interpreted as characters (text) using a character encoding.

  • Trying to read and write files that contain data that is not meant to be interpreted as text, such as *.zip and *.exe files, will lead to problems. You might get an exception while reading the file, for example if the character encoding that converts the bytes to characters encounters a sequence of bytes that cannot be properly interpreted as text. You might also get a corrupted output file, because the conversion from bytes to characters and back to bytes is not always 100% reversible (for complicated reasons).

    If you just want to copy files, then use InputStreams and OutputStreams to do so (that will also work for text files). You can find examples easily by searching.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Mads Nielsen
    Greenhorn

    Joined: Aug 13, 2011
    Posts: 28
    Jesper de Jong wrote:Java has two kinds of classes for I/O:

  • Streams (InputStreams and OutputStreams) are for reading and writing binary data.
  • Readers and Writers are for reading files that can be interpreted as characters (text) using a character encoding.

  • Trying to read and write files that contain data that is not meant to be interpreted as text, such as *.zip and *.exe files, will lead to problems. You might get an exception while reading the file, for example if the character encoding that converts the bytes to characters encounters a sequence of bytes that cannot be properly interpreted as text. You might also get a corrupted output file, because the conversion from bytes to characters and back to bytes is not always 100% reversible (for complicated reasons).

    If you just want to copy files, then use InputStreams and OutputStreams to do so (that will also work for text files). You can find examples easily by searching.


    The problem is that i need to send the file over a socket later, so i need the file data in a format i can send.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18658
        
        8

    Mads Nielsen wrote:The problem is that i need to send the file over a socket later, so i need the file data in a format i can send.


    Okay... but what you send over the socket is bytes. So this requirement of converting binary data to "text" seems to be irrelevant. Where did it come from?
    Mads Nielsen
    Greenhorn

    Joined: Aug 13, 2011
    Posts: 28
    Paul Clapham wrote:
    Mads Nielsen wrote:The problem is that i need to send the file over a socket later, so i need the file data in a format i can send.


    Okay... but what you send over the socket is bytes. So this requirement of converting binary data to "text" seems to be irrelevant. Where did it come from?


    Well, not litteraly text, i just mean i need the file data in a format i can feed to the socket. Bytes will do just fine.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14269
        
      21

    Class Socket has getInputStream and getOutputStream methods. You can use the InputStream and OutputStream that those methods return just like any other InputStream or OutputStream. Data does not necessarily need to be text to be sent over a socket connection.
    Mads Nielsen
    Greenhorn

    Joined: Aug 13, 2011
    Posts: 28
    Jesper de Jong wrote:Class Socket has getInputStream and getOutputStream methods. You can use the InputStream and OutputStream that those methods return just like any other InputStream or OutputStream. Data does not necessarily need to be text to be sent over a socket connection.


    Great, thanks.
    Mads Nielsen
    Greenhorn

    Joined: Aug 13, 2011
    Posts: 28
    I am reading files byte by byte now with DataInputStream.



    How do i create a file containing the bytes i have read from the original file ?

    Kind regards Mads Nielsen
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18658
        
        8

    You simply write the bytes to a FileOutputStream which refers to that file. Don't forget to close the file after you finish.

    And by the way, there are two rules about when to use the "available" method of an InputStream:

    (1) Don't use it unless you know what it does.

    (2) You don't know what it does.

    Read our FAQ article Available Doesnt Do What You Think It Does for a more serious explanation.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14269
        
      21

    What was the reason you chose to use DataInputStream?

    Why are you casting the bytes that you read from the file to char and print them to the console window (line 31)? This will lead to gibberish output on the console.

    You could read the content of the file into a byte array, like in this example. This example shows how to copy a file by reading blocks of data from a FileInputStream and writing those blocks to a FileOutputStream. It's easy to change that example so that you write the bytes to an OutputStream that you get from a socket.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Java: Convert a binary file to "text" and back again.