• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Mads Nielsen
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 21551
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 15627
    46
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 21551
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 15627
    46
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic