-1

I need to be able to have a method scan a line from a file and store this, so that in the driver class the method can be called to store the next line of the file, and then the toString() method of the class can be used to print out the what was scanned. How would I do this?

Jørgen R
  • 9,179
  • 7
  • 36
  • 56
Dale22599
  • 317
  • 1
  • 2
  • 7

2 Answers2

2

Read a text file using java.util.Scanner

File file = new File("file.txt");

try {

    Scanner sc = new Scanner(file);

    while (sc.hasNextLine()) {
        int i = sc.nextInt();
        System.out.println(i);
    }
    sc.close();
} 
catch (FileNotFoundException e) {
    e.printStackTrace();
}
}
Ima Miri
  • 747
  • 11
  • 28
0

I assume that you mean a normal .txt file so have a look at this previous answer here

Community
  • 1
  • 1
Katana24
  • 7,554
  • 16
  • 67
  • 110