Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to Java, Simple I/O question

 
Jay Wa
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is invoice1.txt created by the program or do i have to create it?
Having problems when i run this code the output does not fully display.
The output i get is:

You've ordered 12 units of Java T-shirt
??? Java Mug
??? at $19.99
For a TOTAL of: $239.88

Java IO
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
The DataOutput interface is for doing binary I/O, not human-readable text. The "???" you're seeing are the bits and bytes that represent the numbers internally, the characters you can read. Binary I/O is appropriate for storing data to a compact file or sending it across a network, but not for presenting to a human audience.
I could try to critique your code but I think a better thing to do would be to tell you you should use java.io.PrintWriter instead of DataOutputStream, and that you should go to Sun's I/O tutorial to learn the specifics.
 
Joe Ess
Bartender
Posts: 9300
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

I could try to critique your code but I think a better thing to do would be to tell you you should use java.io.PrintWriter instead of DataOutputStream, and that you should go to Sun's I/O tutorial to learn the specifics.

Ernest, you are usually on the ball (moreso than I), but this example is FROM the IO Tutorial! The problem in this example is that it writes '\n' as an end-of-line marker but on the read it breaks on '\r' (the Windows line seperator is "\r\n"), leaving the file pointer at '\n', which mucks up the later reads. Change the line

char lineSep=System.getProperty("line.separator").charAt(0);

to

char lineSep = '\n';

and the example will work fine.
[ December 09, 2003: Message edited by: Joe Ess ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Joe! Saw those ??? and that DataOutputStream and jumped the gun.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic