-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise1.java
More file actions
33 lines (32 loc) · 999 Bytes
/
Copy pathExercise1.java
File metadata and controls
33 lines (32 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.*;
import java.util.*;
public class Exercise1 {
public static void main(String[] args) {
// Write a program that counts the number of lines of an input file.
FileInputStream file = null;
Scanner sc = null;
int count = 0;
try {
file = new FileInputStream("src/Week5/input.txt");
sc = new Scanner(file);
while (sc.hasNextLine()) {
sc.nextLine();
count++;
}
System.out.println("Number of lines in the file: " + count);
} catch (FileNotFoundException e) {
System.out.println("File not found");
} finally {
try {
if (file != null) {
file.close();
}
if (sc != null) {
sc.close();
}
} catch (IOException e) {
System.out.println("Error closing file");
}
}
}
}