aspose file tools*
The moose likes I/O and Streams and the fly likes New to Java, Simple I/O question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "New to Java, Simple I/O question " Watch "New to Java, Simple I/O question " New topic
Author

New to Java, Simple I/O question

Jay Wa
Greenhorn

Joined: Dec 09, 2003
Posts: 1
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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 ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Thanks, Joe! Saw those ??? and that DataOutputStream and jumped the gun.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New to Java, Simple I/O question