How to read a file into an array in Java?

To read a file into an array in Java, read lines into an ArrayList, then call toArray.

For example, this is how you can read manylines.txt file in the /Users/whaadev directory to an array of strings:

List<String> lines = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader("/Users/whaadev/manylines.txt"))) {
  String line;
  while ((line = reader.readLine()) != null) {
} catch (IOException e) {

String[] linesArray = lines.toArray(new String[0]);