import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class FileReaderExample { public static ArrayList readFileLines(String filename) { ArrayList lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String line; while ((line = reader.readLine()) != null) { lines.add(line); } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); } return lines; } public static void main(String[] args) { ArrayList fileContent = readFileLines("example.txt"); if (!fileContent.isEmpty()) { System.out.println("File content:"); for (String line : fileContent) { System.out.println(line); } } } } The readFileLines method takes a filename and returns an ArrayList of String objects, where each string is a line from the file. BufferedReader is used for efficient reading, and readLine() reads one line at a time. Each read line is added to the ArrayList. 2. Reading All Lines at Once using Files.readAllLines(): For smaller to medium-sized files, Files.readAllLines() provides a concise way to read all lines into a List (which can be assigned to an ArrayList). Java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; public class FilesReadAllLinesExample { public static ArrayList readAllFileLines(String filename) { try { List lines = Files.readAllLines(Paths.get(filename)); return new ArrayList<>(lines); // Convert List to ArrayList } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); return new ArrayList<>(); // Return empty ArrayList on error } } public static void main(String[] args) { ArrayList fileContent = readAllFileLines("example.txt"); if (!fileContent.isEmpty()) { System.out.println("File content:"); for (String line : fileContent) { System.out.println(line); } } } } Files.readAllLines() directly returns a List containing all lines. This List is then converted to an ArrayList. 3. Reading Objects using ObjectInputStream: If the file contains serialized Java objects, ObjectInputStream can be used to read them directly into an ArrayList of those objects. This requires the objects to implement the Serializable interface. Java import java.io.*; import java.util.ArrayList; public class ObjectReaderExample { public static class MyObject implements Serializable { private String name; private int id; public MyObject(String name, int id) { this.name = name; this.id = id; } @Override public String toString() { return "MyObject{name='" + name + "', id=" + id + "}"; } } public static ArrayList readObjectsFromFile(String filename) { ArrayList objects = new ArrayList<>(); try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { objects = (ArrayList) ois.readObject(); } catch (IOException | ClassNotFoundException e) { System.err.println("Error reading objects: " + e.getMessage()); } return objects; } public static void main(String[] args) { // Example: Writing objects to a file first (for demonstration) ArrayList objectsToWrite = new ArrayList<>(); objectsToWrite.add(new MyObject("Alice", 1)); objectsToWrite.add(new MyObject("Bob", 2)); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("objects.ser"))) { oos.writeObject(objectsToWrite); } catch (IOException e) { System.err.println("Error writing objects: " + e.getMessage()); } // Reading objects from the file ArrayList readObjects = readObjectsFromFile("objects.ser"); if (!readObjects.isEmpty()) { System.out.println("Read objects:"); for (MyObject obj : readObjects) { System.out.println(obj); } } } }