File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes file output question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file output question" Watch "file output question" New topic

file output question

Louie Cheung

Joined: Dec 25, 2006
Posts: 17
this code simply takes 2 integer from user and write them on file, but the output file becomes rubbish such as:
how do i fix this? thanks

[edit]Add code tags. CR[/edit]
[ November 17, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46332
Please use the code button; I have edited your post so you can see how much easier it is to read.
You are not getting nonsense; when I edited your post I saw the squares had turned to tiny boxes which look like what I have called code, only smaller.That means you have put 0x0002 into your file, and 0x0002 is a control character. What it actually is, you can work out by looking for "basic Latin" or similar in Unicode. If you pass 2 you get 0x0002. Try running your app with 65 66 67 68 and see whether you get the same as 0x41 0x42 0x43 and 0x44 (same Unicode chart).

What is happening is that you are writing the numbers in binary rather than in text. You need to go through the classes you are using and see whether you are writing binary or text, and whether you should be using different classes. There is more about writing to files etc in the Java Tutorials; I think you want to write a "character stream."
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46332
Actually a trip to the DataOutputStream API will help explain things.
I agree. Here's the link:
subject: file output question
It's not a secret anymore!