Java FileReader (read a file)

7 Views
Published
Java FileReader how to read a file tutorial example

#Java #FileReader #read #file #tutorial #example

public class Main {

public static void main(String[] args) {

// FileReader = read the contents of a file as a stream of characters.
//read() returns an int value which contains the byte value
//when read() returns -1, there is no more data to be read

try {
FileReader reader = new FileReader("art.txt");
int data = reader.read();
while(data != -1) {
System.out.print((char)data);
data = reader.read();
}
reader.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Category
Bro Code
Tags
Java FileReader, java read text file, java file reading and writing
Be the first to comment