Enclosed the value with double quotes if it contains spaces.
The good news is that this is an easy task on Mac OS 1.
For this, we need to wrap the FileReader inside a BufferedReader instance which provides the method readLine().As you know Mac is a wonderful OS, if you are using MacOS, you may need to know how to edit your PATH environment.
Public static void main(String args) throws IOExceptionįileReader fileReader = new FileReader(fileName) Įxample 3: Reading a file line by line using FileReaderįileReader does not directly support reading a file line by line. When all the content of the file has been read, it returns -1 which indicates the end of the file. In the given example, we are using the read() method which reads a single character from the file and returns it. Try(FileReader fileReader = new FileReader(fileName)) Example 2: Reading a file one character at a time This should be used only for small text files. Here we are attempting to read the file in single read() operation so make sure you create a sufficiently large char to store all the content on the file. The file contains 3 small hello world messages. In the given example, we are reading a text file. Let us see a few examples to read a file using the FileReader in Java. FileReader input = new FileReader(fileName, Charset.forName("UTF8")) įileReader input = new FileReader(file, Charset.forName("UTF8")) 3. To specify a different character encoding, we can pass the encoding information as Charset in the second argument to both constructors. Specifying Character EncodingĪbove both examples create the file reader instance with the default character encoding. Creating FileReader using File File file = new File("c:\temp\test.txt") įileReader input = new FileReader(file) 2.3. Creating FileReader using File Name String fileName = "c:\temp\test.txt" įileReader input = new FileReader(fileName) 2.2. For creating the instance of FileReader, use one of its constructors. To use the FileReader in the application, we must first import it from package java.io using the import statement.