• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem while using FileReader to read a file

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic