Java FileReader
Java FileReader
is a class that reads characters from a file in the file system. It is a subclass of the InputStreamReader
class, which is a bridge between byte streams and character streams.
Here's an example of how to use FileReader
to read a file:
try (FileReader reader = new FileReader("example.txt")) { int c; while ((c = reader.read()) != -1) { System.out.print((char) c); } } catch (IOException e) { e.printStackTrace(); }Sowww:ecru.theitroad.com
In this example, we create a new FileReader
object and pass the name of the file we want to read ("example.txt") as a parameter. We then read the file one character at a time using the read()
method, which returns the next character in the file as an integer. We cast this integer to a character and print it to the console. We continue reading characters until the end of the file is reached (which is indicated by the read()
method returning -1).
Note that we use a try-with-resources statement to automatically close the FileReader
when we're done with it, and we catch any IOException
that might occur during the read operation.