Skip to content

Commit 81856ef

Browse files
Count Words, Lines & Characters in a File.java
1 parent 89dfff4 commit 81856ef

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
3+
public class FileStats {
4+
public static void main(String[] args) throws Exception {
5+
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
6+
7+
int lines = 0, words = 0, chars = 0;
8+
String s;
9+
10+
while ((s = br.readLine()) != null) {
11+
lines++;
12+
chars += s.length();
13+
words += s.split(" ").length;
14+
}
15+
16+
System.out.println("Lines: " + lines);
17+
System.out.println("Words: " + words);
18+
System.out.println("Characters: " + chars);
19+
}
20+
}

0 commit comments

Comments
 (0)