Hi i am new to Java working with files. I have got a text file called banking.txt located in C: but when i try put its content into an ArrayList and print it prints out nothing. Is the file path i am using incorrect or is it something different?
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class FileP {
public static void main(String[] args) throws FileNotFoundException {
Scanner read = new Scanner(new File("C:\\banking.txt"));
ArrayList<String> myList = new ArrayList<String>();
while(read.hasNextLine()) {
myList.add(read.nextLine());
}
System.out.println(myList);
}
}
FileandFileReaderin the Scanner constructor trying to read the same system file (on Win 7) - one prints an empty ArrayList, the other prints the file contents. There might be some permissions differences between the two, but I can't tell you what the exact problem is.