import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.List; import java.util.ListIterator; import java.util.ArrayList; public class ReadFile { public static void main(String[] args) { String filePath = "file.txt"; ListfileContents = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { fileContents.add(line); } } catch (IOException e) { e.printStackTrace(); } ListIterator iterator = fileContents.listIterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class SmallestNumber { public static void main(String[] args) { ListIn this example, we use the List listIterator method to iterate through a list of numbers and find the smallest one. This example is not using the java.io package library, but the java.util package instead.numbers = new ArrayList<>(); numbers.add(5); numbers.add(2); numbers.add(10); numbers.add(3); numbers.add(1); int smallestNumber = Integer.MAX_VALUE; ListIterator iterator = numbers.listIterator(); while (iterator.hasNext()) { int currentNumber = iterator.next(); if (currentNumber < smallestNumber) { smallestNumber = currentNumber; } } System.out.println("The smallest number is: " + smallestNumber); } }