forked from Adityaranjanpatra/Btecky2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.java
55 lines (47 loc) · 1.37 KB
/
Counter.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Counter
{
public static void main(String[] args)
{
BufferedReader reader = null;
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try
{
reader = new BufferedReader(new FileReader("D:\\new.txt"));
String currentLine = reader.readLine();
while (currentLine != null)
{
lineCount++;
String[] words = currentLine.split(" ");
wordCount = wordCount + words.length;
for (String word : words)
{
charCount = charCount + word.length();
}
currentLine = reader.readLine();
}
System.out.println("Number of character in file : "+charCount);
System.out.println("Number of words in a file : "+wordCount);
System.out.println("Number of lines in file : "+lineCount);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}