• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic