File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes stream output to reader input? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "stream output to reader input?" Watch "stream output to reader input?" New topic
Author

stream output to reader input?

George Dee-Brown
Ranch Hand

Joined: Mar 15, 2001
Posts: 35
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
 
subject: stream output to reader input?
 
Similar Threads
Basic: How IO reads or write ByteStream and CharacterStream
the argument of method mark() does not work
reader vs stream
DataOutputstream writeshort doesnt work
How to Change the File Name for Each Uploaded Files to the Socket Server?