How do you read the contents of a file into an ArrayList
in Java?
Here are the file contents:
cat
ho
Here's a solution that has worked pretty well for me:
List lines = Arrays.asList(
new Scanner(new File(file)).useDelimiter("\\Z").next().split("\\r?\\n")
);
If you don't want empty lines, you can also do:
List lines = Arrays.asList(
new Scanner(new File(file)).useDelimiter("\\Z").next().split("[\\r\\n]+")
);