• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem while using FileReader to read a file  RSS feed

 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read the contents of a file using FileReader.
Here I am using a read() method to read it.The read() reads character and returns integer in range 0 to 65535.

So I have created int ch,but System.out.println(ch); works and prints the characters without casting it to character.I am confused at this point

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println does a "toString()" on its argument. You can check the source code of the relevant classes (Integer in this case) to see what's involved in that.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehm Ulf? This is a primitive. It just prints the int as it is.

Nelson Sam wrote:prints the characters without casting it to character.

Why would it cast to character automatically? Haven't you declared ch to be an int? you should cast to char manually.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, you're right, there is a PrintStream.println(int). So steeped in Java 5 autoboxing by now that I assumed that println(Object) would be called. Both probably do pretty much the same thing, though.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is, though, fairly unusual to read text data from a file using a FileReader by itself. You can use the FileReader in combination with other classes that make things easier, e.g. BufferedReader:
 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Ehm Ulf? This is a primitive. It just prints the int as it is.

Nelson Sam wrote:prints the characters without casting it to character.

Why would it cast to character automatically? Haven't you declared ch to be an int? You should cast to char manually.


I mean that the program is giving correct output even if I dont cast ch to char.

i.e,

System.out.println(ch);
OR
System.out.println((char)ch);

Both give same results
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Sam, I don't know what to tell you. That's definitely not true; your program will print the Unicode character value for each character as a decimal integer on a separate line -- i.e., for my system, it'll print '48' for the character '0'. Are you sure the code you're running is the code you're looking at? Maybe you got different versions of your program mixed up?
 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest Friedman-Hill.

I compiled and ran the program again and I showed characters only.

So I just created new java file and copied this contents to this file and compiled,run again I got Output in integers.

Here is a snapshot of same program with different outputs





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!