• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

stream output to reader input?  RSS feed

 
Ranch Hand
Posts: 35
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!