- How to Read Files in Java
- Reading Text Files in Java with BufferedReader
- Reading UTF-8 Encoded File in Java with BufferedReader
- Using Java Files Class to Read a File
- Reading Small Files in Java with Files Class
- Reading Large Files in Java with Files Class
- Reading Files with Files.lines()
- Reading Text Files in Java with Scanner
- Reading an Entire File
- Conclusion
- How to Read a File in Java
- Introduction
- What is Stream?
- How to Read a File in Java?
- Different Ways to Read a File Are
- Method 1: Reading a file using the Files.lines() function
- Method 2: Reading a file using the Files.readString() function
- Method 3: Reading a file using the Files.readAllBytes() function
- Method 4: Reading a file using the Files.readAllLines() function
- Method 5: Reading a file using the BufferedReader class
- Method 6: Reading a file using the Scanner class
- Summary
How to Read Files in Java
Throughout the tutorial, we are using a file stored in the src directory where the path to the file is src/file.txt .
Store several lines of text in this file before proceeding.
Note: You have to properly handle the errors when using these implementations to stick to the best coding practices.
Reading Text Files in Java with BufferedReader
The BufferedReader class reads a character-input stream. It buffers characters in a buffer with a default size of 8 KB to make the reading process more efficient. If you want to read a file line by line, using BufferedReader is a good choice.
BufferedReader is efficient in reading large files.
import java.io.*; public class FileReaderWithBufferedReader < public static void main(String[] args) throws IOExceptionbufferedReader.close(); > >
The readline() method returns null when the end of the file is reached.
Reading UTF-8 Encoded File in Java with BufferedReader
We can use the BufferedReader class to read a UTF-8 encoded file.
This time, we pass an InputStreamReader object when creating a BufferedReader instance.
import java.io.*; public class EncodedFileReaderWithBufferedReader < public static void main(String[] args) throws IOException < String file = "src/fileUtf8.txt"; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); String curLine; while ((curLine = bufferedReader.readLine()) != null)< //process the line as you require System.out.println(curLine); >> >
Using Java Files Class to Read a File
Java Files class, introduced in Java 7 in Java NIO, consists fully of static methods that operate on files.
Using Files class, you can read the full content of a file into an array. This makes it a good choice for reading smaller files.
Let’s see how we can use Files class in both these scenarios.
Reading Small Files in Java with Files Class
The readAllLines() method of the Files class allows reading the whole content of the file and stores each line in an array as strings.
You can use the Path class to get the path to the file since the Files class accepts the Path object of the file.
import java.io.IOException; import java.nio.file.*; import java.util.*; public class SmallFileReaderWithFiles < public static void main(String[] args) throws IOException < String file = "src/file.txt"; Path path = Paths.get(file); Listlines = Files.readAllLines(path); > >
You can use readAllBytes() to retrieve the data stored in the file to a byte array instead of a string array.
byte[] bytes = Files.readAllBytes(path);
Reading Large Files in Java with Files Class
If you want to read a large file with the Files class, you can use the newBufferedReader() method to obtain an instance of BufferedReader class and read the file line by line using a BufferedReader .
import java.io.*; import java.nio.file.*; public class LargeFileReaderWithFiles < public static void main(String[] args) throws IOException < String file = "src/file.txt"; Path path = Paths.get(file); BufferedReader bufferedReader = Files.newBufferedReader(path); String curLine; while ((curLine = bufferedReader.readLine()) != null)< System.out.println(curLine); >bufferedReader.close(); > >
Reading Files with Files.lines()
Java 8 introduced a new method to the Files class to read the whole file into a Stream of strings.
import java.io.IOException; import java.nio.file.*; import java.util.stream.Stream; public class FileReaderWithFilesLines < public static void main(String[] args) throws IOException < String file = "src/file.txt"; Path path = Paths.get(file); Streamlines = Files.lines(path); lines.forEach(s -> System.out.println(s)); lines.close(); > >
Reading Text Files in Java with Scanner
The Scanner class breaks the content of a file into parts using a given delimiter and reads it part by part. This approach is best suited for reading content that is separated by a delimiter.
For example, the Scanner class is ideal for reading a list of integers separated by white spaces or a list of strings separated by commas.
The default delimiter of the Scanner class is whitespace. But you can set the delimiter to another character or a regular expression. It also has various next methods, such as next() , nextInt() , nextLine() , and nextByte() , to convert content into different types.
import java.io.IOException; import java.util.Scanner; import java.io.File; public class FileReaderWithScanner < public static void main(String[] args) throws IOException< String file = "src/file.txt"; Scanner scanner = new Scanner(new File(file)); scanner.useDelimiter(" "); while(scanner.hasNext())< String next = scanner.next(); System.out.println(next); >scanner.close(); > >
In the above example, we set the delimiter to whitespace and use the next() method to read the next part of the content separated by whitespace.
Reading an Entire File
You can use the Scanner class to read the entire file at once without running a loop. You have to pass “\\Z” as the delimiter for this.
scanner.useDelimiter("\\Z"); System.out.println(scanner.next()); scanner.close();
Conclusion
As you saw in this tutorial, Java offers many methods that you can choose from according to the nature of the task at your hand to read text files. You can use BufferedReader to read large files line by line.
If you want to read a file that has its content separated by a delimiter, use the Scanner class.
Also you can use Java NIO Files class to read both small and large files.
How to Read a File in Java
In Java Programming, file reading operations play a crucial role in handling and processing data. This article focuses on key methods like Files.lines() , Files.readString() , Files.readAllBytes() , and Files.readAllLines() , along with the BufferedReader and Scanner classes, which provide efficient ways to read files and extract data from different sources.
By understanding these methods and classes, you’ll gain essential knowledge for effective file handling in Java.
Introduction
A file is a storage unit in the computer. All the non-volatile (permanent) data is saved in files. This includes app data, photos, videos, documents, and everything that is stored on our hard disks. Therefore it is very important for us to know how to read these files.
What is Stream?
As we know that conventionally, a Stream is a continuous flow of water, but in Java (and in general programming) Stream is referred to as a continuous flow of data. Therefore, a stream is a sequence of data, in the form of objects. A point to remember is that Stream is not a Data Structure, and it does not store any elements. It is just transporting the data from the source to the destination.
How to Read a File in Java?
For reading a file and performing file operations in Java, we need to import some predefined classes. The following classes are required:
- java.io.File : This class represents a file or directory path in the file system. It provides various methods for file-related operations such as checking file existence, retrieving file information, and manipulating files.
- java.io.FileNotFoundException : This class is an exception that is thrown when an attempt to open a file fails because the file does not exist or cannot be accessed.
By importing these classes, we can utilize their functionalities to handle file operations effectively in Java.
We will include these packages, and other necessary input-output packages in a single line using java.io.* .
Throughout the article, we will be using readThisFile.txt, which is in the directory: C:\Bhavya\Scaler\readThisFile.txt . As we know that ‘\’ is a special character in Java, so we will have to use it with another ‘\’ .
So the directory is «C:\\Bhavya\\Scaler\\readThisFile.txt» .
Contents of this file are :
Different Ways to Read a File Are
Method 1: Reading a file using the Files.lines() function
Files class was introduced in Java 8. It converts the whole file to a Stream of strings.
Files.lines() help us to read the data of the given file, line by line. Files.lines() closes the opened resources automatically (ie, the file), so we do not need to close the file, and we can skip try and catch blocks for Files.lines() .
For using Files.lines() , we will need to define the path of the file. We can use the Paths.get() function Path library. For using this function, we need to include the java.nio.file. package*.
For using the Stream functions, we need to include java.util.stream.* package .
Files.Lines(PATH);
Return Type:
Here we are just printing the contents of the file, but quite often we find ourselves in situations where we need to store and process the data of the file. For storing the data, we can use a list and functions of Lines.
This is useful for small files. If we try to use a large file with this method, it can throw exceptions like java.lang.OutOfMemoryError . We can further optimize it by using the parallel function.
Method 2: Reading a file using the Files.readString() function
The readString method was indeed introduced in Java 11. It provides a convenient way to read the contents of a file and store them as a single string. Internally, Files.readString() utilizes the Files.readAllBytes() function, which we will discuss later in this article. By using readString , we can easily retrieve the entire content of a file as a string without the need for manual byte-to-string conversions.
Note: The readString() method was introduced in Java 11.
Files.readString(PATH);
Return Type:
This method also allows us to read small files. If the size of the file is very large, it may throw java.lang.OutOfMemoryError exception.
Method 3: Reading a file using the Files.readAllBytes() function
The Files.readAllBytes() function was introduced in Java 7. It reads the given file and stores the data in a byte array. (A byte array is a collection of byte data type. It stores the data in the form of bits, since a byte is equal to 8 bits). We can then convert the byte array as we want. As we can see that the data is first stored in a byte array, it is not helpful to use the Files.readAllBytes() function for large files. The Files.readAllBytes() function can throw java.lang.OutOfMemoryError exception if the data is very large.
Files.readAllBytes(PATH);
Return Type:
Here is an example of converting the data from a bytes array to a binary string.
Explanation:
The ASCII code of every character is getting stored in the array, and that is the ouput.
Method 4: Reading a file using the Files.readAllLines() function
The Files.readAllLines was introduced in Java 8. It reads the given file and returns a List of String. As we can see that the data is first stored in a list of strings, it is not helpful to use the Files.readAllLines() function for large files. The Files.readAllLines() function can throw java.lang.OutOfMemoryError exception if the data is very large.
Files.readAllLines(PATH);
Return Type:
Method 5: Reading a file using the BufferedReader class
The BufferedReader class was there in Java from the start and is the fastest method to read the data, from a given file, Line by Line. It is used to read the data using the input stream. As the name suggests, the BufferedReader class buffers the data in the form of small packets of size 8 KB. Since it is only processing the data of 8 KB at a particular time, it is very efficient and can be used for large files.
BufferedReader br = new BufferedReader(new FileReader(PATH));
Explanation:
We are creating a BufferedReader object, and then we can use the methods of BufferedReader class on it.
Since readLine() returns null as we reach the end, we can use it as the stopping condition.
We need to close the file after the reading is done.
Method 6: Reading a file using the Scanner class
After discussing the BufferedReader method, and saying it the fastest why do we need Scanner?
The Scanner class has more useful methods like next() , nextInt() , nextByte() , nextLine() and etc, and we can assign any delimiter to divide the string rather than the default space. Since the Scanner class also reads the data from the stream Line by Line, we can use large text files to read.
Scanner sc = new Scanner(new File(PATH));
Explanation:
We are creating a Scanner object, and then we can use the methods of Scanner class on it.
We need to close the file after the reading is done.
We can also print the whole file without any loops. For printing the entire file at once, without any loops, we will have to use «\\Z» as the delimiter. This delimiter is defined in Java as the end of the input.
Summary
- Files.lines() : The Files.lines() method is used to read all the lines from a file as a Stream . It allows you to efficiently process large files line by line without loading the entire file into memory.
- Files.readString() : The Files.readString() method reads the contents of a file and returns the content as a String . It is a convenient method to read small to medium-sized files where you need the entire content as a single string.
- Files.readAllBytes() : The Files.readAllBytes() method reads all the bytes from a file and returns them as a byte[] array. This method is useful when you need to read binary files or when you want to handle the content of the file as raw bytes.
- Files.readAllLines() : The Files.readAllLines() method reads all the lines from a file and returns them as a List . This method is similar to Files.lines() , but it eagerly loads all the lines into memory, making it suitable for smaller files.
- BufferedReader : The BufferedReader class provides efficient methods for reading character-based input from a stream, such as reading from a file, by buffering the input and minimizing I/O operations.
- Scanner : The Scanner class provides a flexible way to read different types of data from various input sources, including files, streams, and user input, by tokenizing the input and parsing it into appropriate data types.