(SOLVED) Java: FileReader vs. Scanner | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

(SOLVED) Java: FileReader vs. Scanner

If we have Scanner, why would we read file with FileReader?

22nd Aug 2021, 12:53 PM
Xemonix
Xemonix - avatar
3 Answers
+ 2
FileReader is a very low level API that can only read a single character at a time, that's why most commonly it is wrapped inside a BufferedReader, which also add some convenience, e.g. reading whole lines at a time. Scanner is typically slower than BufferedReader because it uses regular expressions to parse the text. https://medium.com/@isaacjumba/why-use-bufferedreader-and-bufferedwriter-classses-in-java-39074ee1a966 However if you need to work with files, I really recommend to check out the more modern java.nio.file API, which is the best way to process files in Java. https://www.baeldung.com/java-nio-2-file-api
22nd Aug 2021, 1:28 PM
Tibor Santa
Tibor Santa - avatar
+ 7
Thanks!
22nd Aug 2021, 1:31 PM
Xemonix
Xemonix - avatar