[Solved] How to read file into an Arraylist and print the ArrayList in Java


The Correct Syntax for Scanner :
Scanner s = new Scanner(new File(“A.txt”) );

Object

In java Object is the superclass of all the classes ie. predefined sucs as String or any user defined .

It will accepts any kind of data such as string or any other types contained in the A.txt file.

========================================================================

import java.io.*;
import java.util.*;
class B{
    public static void main(String aregs[]) throws FileNotFoundException {
         Scanner s = new Scanner(new File("A.txt") );

         ArrayList<Object> list = new ArrayList<Object>();
         while(s.hasNext()) { 
            list.add(s.next());
         }
         System.out.println(list);


    } 
}

solved How to read file into an Arraylist and print the ArrayList in Java