- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Read a File into an ArrayList in Java?
In Java, an ArrayList is a resizable array, which is implemented as part of the Java Collections Framework. It is a dynamic data structure that can hold any type of objects, including primitive types such as integers and characters.
As per the problem statement we have to read a file into an ArrayList. First and foremost we will take the path of the file as input and then we will apply different method to read the file.
Let's start!
For instance
Suppose the input file is “myfile.txt”.
After reading the content of the file, the result will be:
The contents of the file are: [Hello everyone, I am a coder.]
Algorithm
Step-1: Create the instance of ArrayList.
Step-2: Declare and initialize the file path.
Step-3: Read the content of the file.
Step-4: Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using FileReader and BufferReader
By Using Scanner
Let's see the program along with its output one by one.
Note: This program will not work in any online Java compiler as it can not access local file paths from online compilers.
Approach-1: By Using FileReader and BufferReader
In this approach, path of file is taken as input by FileReader and then it is read line by line using BufferReader.
Example
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class Main { //main method public static void main(String[] args) { //instance of ArrayList ArrayList<String> lines = new ArrayList<>(); //try & catch block to handle exception try { //taking the file path as input using FileReader FileReader fileReader = new FileReader("C:/Users/ Desktop/ Program/myfile.txt"); //reading the content of the file using BufferedReader BufferedReader bufferedReader = new BufferedReader(fileReader); String line; //storing the content of the file in String while ((line = bufferedReader.readLine()) != null) { lines.add(line); } bufferedReader.close(); fileReader.close(); } catch (IOException e) { System.out.println("An error occurred while reading the file."); e.printStackTrace(); } //print the result System.out.println("The contents of the file are: " + lines); } }
Output
The contents of the file are: [Hello everyone, I am a coder.]
Approach-2: By Using User Defined Method
In this approach, path of file is taken as input by File and then it is read line by line using Scanner.
Example
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class Main { //main method public static void main(String[] args) { //instance of ArrayList ArrayList<String> lines = new ArrayList<>(); try { //taking the file path as input using File File file = new File("example.txt"); //reading the content of the file using Scanner Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { //storing the content of the file in String String line = scanner.nextLine(); lines.add(line); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred while reading the file."); e.printStackTrace(); } //print the result System.out.println("The contents of the file are: " + lines); } }
Output
The contents of the file are: [Hello Users, Let's code.]
In this article, we explored different approaches to read a file into an ArrayList by using Java programming language.