Java R&W Related

In Java, byte = 8 bit, char = 16 bit

In C/C++, char = 8 bit

There is difference because Java uses Unicode, however C uses ASCII as basic character collection.

Java uses InputStream to read BYTE-DATA, to cover InputStream with InputStreamReader to read CHAR-DATA, to cover InputStreamReader with BufferedStream to read line of chars. So java have to recomprehend the byte infomation into char information and into line-chars information with a 2-step conversion.

Rembember the following:

size:   8 bit -> 16 bit -> line -> file

mode: byte -> char -> line -> file

read:  IS -> ISR -> BR -> String

write: OS -> OSW ->( *BW ->) PW -> String

Why do we have the parenthesized BW part?, see the official doc:(Java SE 8)

In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

 PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
 

will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

 When reviewing some test programs that I wrote following the java tutorial, find something like:

1 FileOutputStream out = new FileOutputStream("1.txt");
2 //Considering using the following method instead
3 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("1.txt"));

Checked Java SE 8 official API, that BOS method exists! Gee.. And, that's the exact usage of BOS.. Gee again...

posted on 2016-04-18 12:31  三叁  阅读(176)  评论(0编辑  收藏  举报

导航