aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Some questions about writeBytes(String) vs writeChars(String) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Some questions about writeBytes(String) vs writeChars(String)" Watch "Some questions about writeBytes(String) vs writeChars(String)" New topic
Author

Some questions about writeBytes(String) vs writeChars(String)

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
I know this is not the best way to deal writing and reading text from a file ( should buffer stuff or use FileReader/Writer etc ) but I'm trying to understand what is going on here.
So I have this straight, all information when written to a file is just really a stream of bytes written to the file. So that when you use DataInputStream and the various readXXX methods you are just reading back the correct number of bytes needed to make the XXX datatype. If this is the case what am I doing wrong, or how come in the following code I wrote when I do writeBytes(String s) versus writeChars(String s ) I get back a bunch of question marks when I read from the file using a loop and doing readChar() ? I would think that readChar() wouldn't care how the bytes were written to the file. Possibly though the problem is in using writeBytes( Sting s )? Maybe when you do writeBytes( String s ) it's not writing the 2 byte representation of the chars to the file? I'm a bit confused here.
Also, what is wrong with my construct in the while loop for getting back the chars? Is there no way around it throwing at a EOFException when it gets to the end? ( I know this should be buffered in a BufferedOutputStream, etc. but I'm trying to get some of these basics understood).
Thanks for any help.

and if you want it without the UBB code for cut and paste help:
import java.io.*;
class TestIOStuff {
static char c;
public static void main( String[] args ) throws IOException {
String s = "Some boring String Line";
File file = new File("testing.txt");
FileOutputStream fo = new FileOutputStream( file );
DataOutputStream out = new DataOutputStream( fo );
out.writeChars( s );
//out.writeBytes( s );
out.close();

FileInputStream fi = new FileInputStream( file );
DataInputStream in = new DataInputStream( fi );
try {
while( (c = in.readChar()) != -1 ) {
System.out.print( c );
}
}
catch( Exception e ) {
System.out.println( "\n"+e );
}
in.close();
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Rick,
The writeBytes write out only the low order byte from the character. That is the problem with you using writeBytes and trying to read in char.
You are not doing anything wrong with the read loop. It is working correctly. You can't use the readChar method to check for EOF so you need to use a try/catch as you are doing. The last good value is the last letter and then you are trying to read in another letter and the character (2 bytes) don't exist.
Regards,
Manfred.
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by Manfred Leonhardt:
Hi Rick,
The writeBytes write out only the low order byte from the character. That is the problem with you using writeBytes and trying to read in char.

Thanks Manfred. Duh <blushing>, even though I looked at the API a million times I kept missing the part where it says it throws out the high eight bits like you mentioned.
Wonder what use anyone would have for this writeBytes(String s) method? You'd write it to a file completely messed up. I guess maybe someone could have a char String where all the chars fell within the first low order bit range and you'd be ok.
Also curious why they didn't provide the constructor
FileOutputStream( File f, boolean append ) and just provided the constructor:
FileOutputStream( String s, boolean append ).
I understand you can't have a constructor for every situation but this sure would seem like a common one that could be used, unless of course, I'm missing something. I guess it's not that big of a deal as you could always go
FileOutputStream( myFile.getPath(), booleanVal )
Thanks again Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some questions about writeBytes(String) vs writeChars(String)
 
Similar Threads
File Descriptor
about DataInput streams (chains)
Reading Tabs
File and FileDescriptor
New to Java, Simple I/O question