-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDictionaryManagement.java
More file actions
100 lines (87 loc) · 2.79 KB
/
Copy pathDictionaryManagement.java
File metadata and controls
100 lines (87 loc) · 2.79 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
public class DictionaryManagement {
public static final Scanner scanner = new Scanner(System.in);
public String insertFromCommandline()
{
String word_target;
word_target = scanner.nextLine();
word_target = word_target.trim();
word_target = word_target.toLowerCase();
return word_target;
}
public String dictionaryLookup(Map<String,String>map)
{
DictionaryManagement dictionaryManagement = new DictionaryManagement();
System.out.println("Nhập từ cần dịch(Anh-Việt) ");
String enterWord = dictionaryManagement.insertFromCommandline();
String s;
if(map.containsKey(enterWord))
{
s = enterWord + " : " + map.get(enterWord);
}
else
{
s = "Không có từ cần tra";
}
return s;
}
public void dictionaryExportToFile(Map<String,String>map)
{
File file = new File("1.txt");
FileWriter fr = null;
BufferedWriter br = null;
try{
fr = new FileWriter(file);
br = new BufferedWriter(fr);
for(Map.Entry<String,String>entry: map.entrySet()) {
String dataWithNewLine = entry.getKey()+" "+entry.getValue()+System.getProperty("line.separator");
br.write(dataWithNewLine);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void removeWord(Map<String,String> map)
{
System.out.print("Nhập từ Tiếng Anh muốn xóa: ");
String s = scanner.nextLine();
if(map.containsKey(s))
{
map.remove(s);
}
else
{
System.out.println("Từ "+ s + " Không có trong từ điển để xóa");
}
}
public void addWriteFile(String s)
{
try {
File file = new File("dictionaries.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getName(), true);
BufferedWriter bw = new BufferedWriter(fw);
String data = s + System.getProperty("line.separator");
bw.write(data);
bw.close();
System.out.println("Đã Thêm thành công");
}
catch (Exception e) {
e.printStackTrace();
}
}
}