Skip to content

Commit 55efac1

Browse files
committed
Added practical 6.1
1 parent 30020a9 commit 55efac1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Practical6/practical1.java

+28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
package Practical6;
22

3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
37
public class practical1 {
48
public static void main(String[] args) {
9+
if (args.length == 0) {
10+
System.out.println("Error: No files provided as arguments.");
11+
return;
12+
}
13+
14+
for (String fileName : args) {
15+
try {
16+
int lines = countLinesInFile(fileName);
17+
System.out.printf("The number of lines in %s is %d\n", fileName, lines);
18+
} catch (IOException e) {
19+
System.out.printf("Error reading file %s: %s\n", fileName, e.getMessage());
20+
} catch (Exception e) {
21+
System.out.printf("Unexpected error occurred with file %s: %s\n", fileName, e.getMessage());
22+
}
23+
}
24+
}
525

26+
private static int countLinesInFile(String fileName) throws IOException {
27+
int lines = 0;
28+
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
29+
while (br.readLine() != null) {
30+
lines++;
31+
}
32+
}
33+
return lines;
634
}
735
}

0 commit comments

Comments
 (0)