-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUse_of_bufferreader_and_writer.java
More file actions
76 lines (67 loc) · 2.56 KB
/
Use_of_bufferreader_and_writer.java
File metadata and controls
76 lines (67 loc) · 2.56 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package samir_sirs_class;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
public class Use_of_bufferreader_and_writer {
public static void main(String[] args) throws IOException, FileNotFoundException, NullPointerException {
int res = 0, ans = 0, sum = 0;
FileWriter fw = new FileWriter("test.txt");
BufferedWriter bf = new BufferedWriter(fw);
bf.write("rakib emon rakib pulok ranak shouharda jogada rakib jogada pulok");
bf.write("\n");
bf.close();
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println("your given string is\n" + br.readLine() + "\n\n");
try {
br = new BufferedReader(new FileReader("test.txt"));
String s;
String[] words = null;
while ((s = br.readLine()) != null) {
words = s.split(" ");
int count = 1;
sum = words.length;
for (int i = 0; i < words.length; i++) {
if (words[i].equals("")) {
continue;
}
for (int j = i + 1; j < words.length; j++) {
if (words[i].equals(words[j])) {
words[j] = "";
count++;
}
}
res = res + count;
System.out.println("'" + words[i] + "'" + " presents " + count + " times.");
words[i] = "";
sum--;
count = 1;
}
}
sum = words.length - sum;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileReader fr1 = new FileReader("test.txt");
BufferedReader br1 = new BufferedReader(fr1);
String str = br1.readLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {
ans++;
}
}
System.out.println("\n\ntotal words " + res);
System.out.println("\n\ntotal characters " + ans);
System.out.println("\n\ntotal uniqe words " + sum);
}
}