Create BufferedReader from InputStreamReader to read text file - Java File Path IO

Java examples for File Path IO:Text File

Description

Create BufferedReader from InputStreamReader to read text file

Demo Code


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class InputStreamReaderTest {

    public static void main(String[] args) throws IOException{
        InputStream inputStream = null;
        Reader reader = null;/*from ww w. j  a  v a 2s .co m*/
        BufferedReader buffer = null;
        try{
            inputStream = new FileInputStream("C:/input.txt");
            reader = new InputStreamReader(inputStream);
            buffer = new BufferedReader(reader);
            int data;
            while(true){
                data = buffer.read();
                if(data== -1){
                    break;
                }
                System.out.println("char --> " + (char)data);                
            }
        }finally{
            if(reader != null){
                reader.close();
            }
            if(inputStream != null){
                inputStream.close();
            }
        }
    }
    
}

Related Tutorials