• Post Reply Bookmark Topic Watch Topic
  • New Topic

IO-read() write() methods

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing basic programs of Java IO

I read , read() ,write() methods of both ,Stream-classes & Character IO
work with ints .
read() returns int so must be caught in int variable.
To write(),we write int.
now i have 2 Questions .
1.
when i try wrting char in write(),it doesnt show any error ,even when we dont have any constructor ,showing ,chars.

2.
when i try writing some int variable in file,file itself shows some cryptic text.why


thnkx
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you try to pass a char value (for eg: A) its int equivalent (65) is passed.

and for write() try passing values (65 to 90) and (97 to 122)
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucky J Verma:
I am doing basic programs of Java IO

I read , read() ,write() methods of both ,Stream-classes & Character IO
work with ints .
read() returns int so must be caught in int variable.
To write(),we write int.
now i have 2 Questions .
1.
when i try wrting char in write(),it doesnt show any error ,even when we dont have any constructor ,showing ,chars.

2.
when i try writing some int variable in file,file itself shows some cryptic text.why


thnkx


char gets converted to int automatically according to

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.


Regarding 2: While writing , you are just writing bytes to filesystem and while reading the text editor uses the system encoding along with the bytes from the filesystem to show you the characters.In case the bytes are not recognizable by your character set then you will get junk characters.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!