java scan a file

Solutions on MaxInterview for java scan a file by the best coders in the world

showing results for - "java scan a file"
Fatoumata
09 Jan 2017
1import java.io.File;
2import java.util.Scanner;
3
4public class ReadFile {
5
6    public static void main(String[] args) {
7
8        try {
9            System.out.print("Enter the file name with extension : ");
10
11            Scanner input = new Scanner(System.in);
12
13            File file = new File(input.nextLine());
14
15            input = new Scanner(file);
16
17
18            while (input.hasNextLine()) {
19                String line = input.nextLine();
20                System.out.println(line);
21            }
22            input.close();
23
24        } catch (Exception ex) {
25            ex.printStackTrace();
26        }
27    }
28
29}
30