FileReader class

                                          
    java.lang.Object                                     
     |                                    
     |--java.io.Reader                                 
         |                                
         |--java.io.InputStreamReader                             
             |                            
             |--java.io.FileReader                         
                                          

Class for reading character files.

FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

ConstructorSummary
FileReader(File file)Creates a new FileReader, given the File to read from.
FileReader(FileDescriptor fd)Creates a new FileReader, given the FileDescriptor to read from.
FileReader(String fileName)Creates a new FileReader, given the name of the file to read from.

Methods inherited from class java.io.InputStreamReader

ReturnMethodSummary
voidclose()Closes the stream and releases any system resources associated with it.
StringgetEncoding()Returns the name of the character encoding being used by this stream.
intread()Reads a single character. The character read, or -1 if the end of the stream has been reached
intread(char[] cbuf, int offset, int length)Reads characters into a portion of an array. The character read, or -1 if the end of the stream has been reached
booleanready()Tells whether this stream is ready to be read.

Methods inherited from class java.io.Reader

ReturnMethodSummary
voidmark(int readAheadLimit)Marks the present position in the stream.
booleanmarkSupported()Tells whether this stream supports the mark() operation.
voidreset()Resets the stream.
longskip(long n)Skips characters.
Revised from Open JDK source code

import java.io.FileReader;

public class Main {
  public static void main(String[] argv) throws Exception {
    FileReader fr = new FileReader("text.txt");

    int ch;
    do {
      ch = fr.read();
      if (ch != -1)
        System.out.println((char) ch);
    } while (ch != -1);
    fr.close();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.