Read a Text File
You can open a text file and read each line.
- The
BufferedReader
represents a text file. - You can create
BufferedReader
from an
FileReader
.
- The while condition
(s=fh.readLine())!=null
does two
things, it assigns the string s and it checks for null.
The text file [haggis.txt] includes some lines of text.
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Using the try structure
You can use the try feature in Java 7. This ensures that the file will be closed and disposed of even if the read fails.
- If the file is missing then the exception will be raised and caught.
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Using the try structure
You can use the streams from Java 8.
- In the forEach statement there is a lambda expression - s is a string in this
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]