• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

stream output to reader input?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic