File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes InputStreamReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "InputStreamReader" Watch "InputStreamReader" New topic
Author

InputStreamReader

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 606


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

Joined: Oct 27, 2005
Posts: 19543
    
  16

What is the indication for a new line? If you can answer that question, simply read the characters until you reach that indication.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Use a FileReader rather than an input stream for a text file.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Oct 13, 2005
Posts: 36486
    
  16
Agree: Rob is right.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
. . . 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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Dec 31, 2010
Posts: 10
i am not sure why, but why can't you use the Scanner?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InputStreamReader
 
Similar Threads
java io question, conceptual query
IO Question from javacaps
Copy file
Read file
system.in.read()