• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file output question

 
Louie Cheung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50176
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50176
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually a trip to the DataOutputStream API will help explain things.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic