-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_read_operations_29.java
More file actions
31 lines (25 loc) · 971 Bytes
/
Copy pathfile_read_operations_29.java
File metadata and controls
31 lines (25 loc) · 971 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class file_read_operations_29 {
public static void main(String[] args) throws IOException {
String fileName = "example.txt";
FileReader fileReader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(fileReader);
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
String line;
while ((line = reader.readLine()) != null) {
charCount += line.length();
String[] words = line.split("\\s+");
wordCount += words.length;
lineCount++;
}
System.out.println("Number of characters: " + charCount);
System.out.println("Number of words: " + wordCount);
System.out.println("Number of lines: " + lineCount);
reader.close();
fileReader.close();
}
}