← Back to Gists

Read file lines

📝 Java
adamterrell168
adamterrell168 · 19d ago
Reads all lines from a file into a list, handling exceptions with a try-catch block.
Java
import java.nio.file.;
import java.util.;
import java.io.IOException; List<String> lines = new ArrayList<>();
try { lines = Files.readAllLines(Paths.get("file.txt"));
} catch (IOException e) { System.err.println("Error reading file: " + e.getMessage());
}

Comments

0
Consider using a `with` statement for automatic file closing instead of manual try-catch.