-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkWithFile.java
More file actions
51 lines (45 loc) · 1.65 KB
/
Copy pathworkWithFile.java
File metadata and controls
51 lines (45 loc) · 1.65 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
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class workWithFile{
Map<Character, Integer> counter = new HashMap<>();
Path name;
public workWithFile(Path path){
this.name = path;
}
public void openFile() throws NullPointerException, IOException {
File file = new File(this.name.toUri());
BufferedReader br = new BufferedReader(new FileReader(file));
while(true){
String q = br.readLine();
if(q == null){
break;
}
byte[] str = q.getBytes();
for (byte b : str) {
if(b < 0){
continue;
}
if (counter.containsKey((char) b)) {
counter.put((char) b, counter.get((char) b) + 1);
} else {
counter.put((char) b, 1);
}
}
}
System.out.println(counter.toString());
}
public void writeToFile() throws IOException {
for (Character i : counter.keySet()) {
String g = "Символ " + "'" + i + "'" + " встречался в данном файле " + counter.get(i) + " раз.";
Files.write(name, Collections.singleton(g), StandardCharsets.UTF_8, StandardOpenOption.APPEND);
}
System.out.println("Запись в файл успешно произведена.");
System.out.println("Расположение файла: " + name);
}
}