This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

read() method working in byte stream and character stream classes  RSS feed

 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the exact difference between read() method of FileInputStream and FileReader class?I tried to read and write a character with unicode valueFfE6 i.e. 16 bit,read() of both the classes was able to read the 16 bit char successfully.Then why is it said that read() of FileInputStream class holds a byte value?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonali raina wrote:I tried to read and write a character with unicode valueFfE6 i.e. 16 bit,read() of both the classes was able to read the 16 bit char successfully.


Sorry but that can't be the whole story since, except for the EOF value, the range of values you can obtain from FileInputStream.read() is 0 to 0xFF inclusive ! Please show the code you used to get the value 0xF1E6 from FileInputStream.read() .
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the code from http://docs.oracle.com/javase/tutorial/essential/io/charstreams.html
http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html
but in the xanadu.docx file I copied the characters from character-map(in accessories).In character map select character set:Unicode and try to use characters with larger unicode value(say FE22)
Also use docx file because simple txt file doesnot support unicode encoding.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I really don't understand what code you used to get byte values of 0xFF22 since nothing in those references can possibly generate byte values out of the range 0 to 0xFF !
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply Richard Tookey
I have used exactly the same code(CopyBytes) which is given in http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html link.
That is I am trying to copy characters from "xanadu.docx" to "outagain.docx".
But the real confusion is that even chinese characters (like "full width won sign ₩"and "full width Yen sign ¥") are also read and copied successfully from xanadu to outagain; which i think should not happen because chinese characters are represented in 16 bits in unicode encoding and FileInputStream.read() reads only 8 bits.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonali raina wrote:
I have used exactly the same code(CopyBytes) which is given in http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html link.
That is I am trying to copy characters from "xanadu.docx" to "outagain.docx".


Sorry but that code copies bytes and not characters and nothing in it prints the bytes being transferred. Since in a file, depending on the character encoding, all characters are represented as one or my bytes I don't understand your problem. Copying the bytes from a file to a file is in effect copying the characters. It is only within memory where one actually has a 'char' primitive and the conversion of bytes to a char is not involved at all in the code you cite.

I suspect someone else will have to respond since I really don't understand your problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!