File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stream output to reader input?

 
George Dee-Brown
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IOers,
I am correct in stating that a Reader reads 1 byte at a time? I tought it read by default 1 unicode (16 bit) char at a time. I wrote the following very simple test program to write (using a stream) a byte followed by 3 chars. When I read these using a reader it seems to read one byte at a time (8 bits) not 16!
import java.io.*;
public class Test5
{
public static void main(String[] args)
{
File f = new File("io2.txt");

try {
FileOutputStream fos = new FileOutputStream (f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream (bos);

byte x = 69;
dos.writeByte(x);
dos.writeChars("ABC");
dos.close();
}
catch (IOException ioe) {System.out.println(ioe);}

try {
FileReader fr = new FileReader (f);
BufferedReader br = new BufferedReader (fr);
System.out.println((char)br.read()); // prints E
System.out.println((char)br.read()); // prints nothing
System.out.println((char)br.read()); // prints A
System.out.println((char)br.read()); // prints nothing
System.out.println((char)br.read()); // prints B
System.out.println((char)br.read()); // prints nothing
System.out.println((char)br.read()); // prints C
br.close();
}

catch (IOException ioe) {System.out.println(ioe);}

}
}
regards,
George
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic