Hello guys, welcome to my blog. Today, we'll discuss another interesting Java interview question, BufferedReader vs Scanner. It's not only important from an interview point of view but also to work efficiently with Java. Even though both BufferedReader and Scanner can read a file or user input from the command prompt in Java, there are some significant differences between them. One of the main differences between BufferedReader and Scanner class is that the former class is meant to just read String or text data while the Scanner class is meant to both read and parse text data into Java primitive types like int, short, float, double, and long.
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
How to read a file line by line in Java? BufferedReader Example
Hello Java Programmers, if you are looking for a way to read a file line by line in Java then don't worry, Java provides java.io package in JDK API for reading File in Java from File system e.g. C:\ or D:\ drive in Windows or any other directory in UNIX. First, you can use FileInputStream to open a file for reading. FileInputStream takes a String parameter which is a path for the file you want to read. Be careful while specifying File path as path separator is different on Window and UNIX. Windows uses backslash while UNIX uses forward slash as a path separator.
How to read file in Java using Scanner Example - text files
Reading a file with Scanner
From Java 5 onwards java.util.Scanner class can be used to read
file in Java. Earlier we have seen examples of reading
file in Java using FileInputStream and reading
file line by line using BufferedInputStream and in this Java tutorial, we
will See How can we use Scanner to read files in Java. Scanner is a
utility class in java.util package and provides several
convenient methods to read int, long, String, double etc from a source which can be an InputStream,
a file, or a String itself.
Subscribe to:
Posts (Atom)