Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InputStreamReader

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I want to read mytext.txt line by line, As string, Without using any buffer class or buffer array. How to do that?

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the indication for a new line? If you can answer that question, simply read the characters until you reach that indication.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a FileReader rather than an input stream for a text file.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree. FileReader uses the system encoding; this is the proper way to read from a file using a specific encoding, in this case UTF8.

In fact, FileReader extends InputStreamReader; its constructors each call super(new FileInputStream(arg)) where arg is the argument to the constructor.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: Rob is right.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but I don't think it's "UTF8". I think it's "UTF-8", but you would have to check carefully, maybe here.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java you can use both for charsets. One is an alias for the other, but I can't remember which way around it is.
 
Kurosaki Ichigo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not sure why, but why can't you use the Scanner?

 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:In Java you can use both for charsets. One is an alias for the other, but I can't remember which way around it is.
Thank you. Wasn't certain about that. I think the official name is UTF-8, so UTF8 would be the alias.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic