java loop through file

Solutions on MaxInterview for java loop through file by the best coders in the world

showing results for - "java loop through file"
Vanessa
23 Jun 2018
1/*
2 *	FileLooper reads data from file and adds it to an ArrayList class
3 *	for later use.
4 */
5import java.util.*;
6import java.io.*;
7
8class FileLooper {
9  public static void main(String [] args) {
10    String fileName = "file.txt";
11    ArrayList<String> fileData = new ArrayList<>();
12    Scanner file = new Scanner(new File(fileName));
13    
14    while (file.hasNext()) {
15       fileData.add(file.next());
16    }
17  }
18}