Results 1 to 4 of 4
  1. #1
    Rybo is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Exclamation Converting a string of binary to data, to save to a file

    Hello everyone. Recently, I've been working on the task of converting a file to a string of binary, converting that string of binary to a sound, and then converting that back to a string of binary. The problem is, after I convert back to binary, I can't figure out how to convert that binary string to data so I can save it to a file, and re-create the file. The reason I'm not posting and code for this post is I literally have no idea on how to go about saving a string of binary to a file. I did a good 20 minutes of searching and came up with no results for this issue. Any help would be very appreciated.
    Ex, if I had
    Java Code:
    101010011001011110011111010011000111000011100001110000111000011100001110000111000011100001110000111000011100001110000111001011001011001011001011001011001011001011001011001011001011001011001011001011
    0001110001110001110001110001110001110001110001110001110001
    As a string, how would I convert that string of binary into the original file?

  2. #2
    RahulMux is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Converting a string of binary to data, to save to a file

    Use obJectOutputStream

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Converting a string of binary to data, to save to a file

    Quote Originally Posted by Rybo View Post
    As a string, how would I convert that string of binary into the original file?
    That depends on the content of the original file, i.e. if the original content were a bunch of ints, I'd break up the String into 32 bit chunks and convert each to an int; but it all depends on the way the original file was encoded into a binary String ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    RahulMux is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Converting a string of binary to data, to save to a file

    Sample Code of Dealing with binary file

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.Date;

    public class ObjectIOApp {
    public static void main(String args[]) throws IOException, ClassNotFoundException {
    File file = new File("test.txt");
    FileOutputStream outFile = new FileOutputStream(file);
    ObjectOutputStream outStream = new ObjectOutputStream(outFile);
    TestClass1 t1 = new TestClass1(true, 9, 'A', 0.0001, "java");
    TestClass2 t2 = new TestClass2();
    String t3 = "This is a test.";
    Date t4 = new Date();
    outStream.writeObject(t1);
    outStream.writeObject(t2);
    outStream.writeObject(t3);
    outStream.writeObject(t4);
    outStream.close();
    outFile.close();
    FileInputStream inFile = new FileInputStream(file);
    ObjectInputStream inStream = new ObjectInputStream(inFile);
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    inStream.close();
    inFile.close();
    file.delete();
    }
    }

Similar Threads

  1. converting binary to string
    By stegano in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 07:20 PM
  2. GUI: How to save these data to a text/dat file?
    By florachu2121 in forum New To Java
    Replies: 6
    Last Post: 08-01-2010, 11:31 PM
  3. Create a form, input some data and save to file
    By cselic in forum AWT / Swing
    Replies: 5
    Last Post: 05-07-2010, 12:28 PM
  4. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  5. Converting a text file int binary
    By sruthi_2009 in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •