File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1
1
package Practical6 ;
2
2
3
+ import java .io .BufferedReader ;
4
+ import java .io .FileReader ;
5
+ import java .io .IOException ;
6
+
3
7
public class practical1 {
4
8
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
+ }
5
25
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 ;
6
34
}
7
35
}
You can’t perform that action at this time.
0 commit comments