java add fileconecnt to arraylist code example

Example 1: java import text file into arraylist

List<String> list = Files.readAllLines(new File("input.txt").toPath(), Charset.defaultCharset() );

Example 2: java import text file into arraylist

Scanner s = new Scanner(new File("filepath"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
    list.add(s.next());
}
s.close();

Example 3: java read integer from text file into array scanner

Scanner scanner = new Scanner(new File("input.txt"));
int [] tall = new int [100];
int i = 0;
while(scanner.hasNextInt())
{
     tall[i++] = scanner.nextInt();
}

Tags:

Java Example