Results 1 to 11 of 11
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default writing to binary files

    Hi,

    I'm currently using ObjectOutputStream.writeInt() to write to binary files.

    It works if the user types an integer. But obviously fails if the user types in a character string.

    Is there a way to write to a binary file without having to check each character to see if it's a string or int, double, etc?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by TopNFalvors View Post
    I'm currently using ObjectOutputStream.writeInt() to write to binary files.
    It works if the user types an integer. But obviously fails if the user types in a character string.
    Obviously? This makes no sense.

    Is there a way to write to a binary file without having to check each character to see if it's a string or int, double, etc?
    You don't have to check any of that. If you are writing an ObjectOutputStream though, you need to make sure that you write out objects that implement a Serializable interface. Otherwise your question is very short on necessary details. Please see my link below on how to ask smart questions to see what details I'm talking about that will make your question in fact answerable.

  3. #3
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Your're right, that makes no sense.

    I'm writing data into the binary file like this:

    Java Code:
    Scanner myScanner = new Scanner(System.in);
    int intTapeKey = 0;
    
    System.out.println("Provide tape key:");
    intTapeKey = myScanner.nextInt();
    Is there a better way to enter in binary data? Because if the user enters in a character, it will obviously fail(due to the nextInt statement).

    Thanks!

  4. #4
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Ok, I solved the writing binary problem I was having.

    Here's what I'm doing now that seems to be working:

    Java Code:
    myStream = new ObjectOutputStream(new FileOutputStream(myFile));
    Scanner myScanner = new Scanner(System.in);
    String tapeKey = "";
    
    System.out.println("Provide tape key:");
    tapeKey = myScanner.nextLine();
    
    myStream.writeObject(tapeKey);
    Thanks

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    I'm lost here.
    What do you mean by a binary file?
    Why are you using an ObjectOutputStream for anything other than outputting an Object (ie serilising an object to somewhere)?

  6. #6
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Well I'm writing to a file called key.bin that stores data in binary format to be read by a key-reading program.

    Thanks

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    And does that key-reading program use an ObjectInputStream?

    It's just that that stream strikes me as plain wrong for what it appears you're doing.

  8. #8
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Well I'm trying to read and write to a binary file...not a normal, human-readble text file.

    I figured ObjectInputStream and ObjectOutputStream would be best.

    Should I use something else?

    Thanks

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    FileOutputStream.
    write(). Well one of the write() methods anyway.

    You only want the bytes, but the ObjectOutputStream will print lots of stuff to do with the object you're writing.

    The following:
    Java Code:
        public static void main(String[] args) {
            try {
                ObjectOutputStream oos = new ObjectOutputStream(System.out);
                int i = 10;
                oos.writeObject(10);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    produces:
    Java Code:
    (garbage)sr(garbage)java.lang.Integer(garbage)
    etc etc. Which the ObjectInputStream would use to recreate an Integer object of value 10.

    Now, I doubt that's what you want.

  10. #10
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    I tried one version that wrote/read bytes, but when reading back, it didn't read back in plain english...it was the human-unreadble binary.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Yes.
    That's what a binary file is.

    If you want to write to a bin then you have to expect it to be a bin when read back.

Similar Threads

  1. Reading / Writing files
    By Learning Java in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 09:21 PM
  2. Converting mp3 files to binary
    By kylefrank in forum Advanced Java
    Replies: 4
    Last Post: 06-09-2010, 08:25 PM
  3. Read binary files
    By ronaldo121 in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 03:37 AM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  5. Applets writing to files
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 08:45 AM

Posting Permissions

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