Results 1 to 11 of 11
Thread: writing to binary files
- 03-02-2011, 07:37 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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
-
Obviously? This makes no sense.
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.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?
- 03-03-2011, 12:26 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Your're right, that makes no sense.
I'm writing data into the binary file like this:
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).Java Code:Scanner myScanner = new Scanner(System.in); int intTapeKey = 0; System.out.println("Provide tape key:"); intTapeKey = myScanner.nextInt();
Thanks!
- 03-03-2011, 03:46 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Ok, I solved the writing binary problem I was having.
Here's what I'm doing now that seems to be working:
ThanksJava 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);
- 03-03-2011, 04:28 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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)?
- 03-03-2011, 04:33 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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
- 03-03-2011, 04:37 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 03-03-2011, 04:52 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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
- 03-03-2011, 04:59 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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:
produces: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(); } }
etc etc. Which the ObjectInputStream would use to recreate an Integer object of value 10.Java Code:(garbage)sr(garbage)java.lang.Integer(garbage)
Now, I doubt that's what you want.
- 03-03-2011, 06:10 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
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.
- 03-04-2011, 08:29 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
Reading / Writing files
By Learning Java in forum New To JavaReplies: 6Last Post: 08-08-2010, 09:21 PM -
Converting mp3 files to binary
By kylefrank in forum Advanced JavaReplies: 4Last Post: 06-09-2010, 08:25 PM -
Read binary files
By ronaldo121 in forum New To JavaReplies: 5Last Post: 03-04-2010, 03:37 AM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM -
Applets writing to files
By bugger in forum New To JavaReplies: 2Last Post: 11-20-2007, 08:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks