Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lien2512 authored Oct 13, 2018
1 parent aba6be6 commit d2f7b46
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
74 changes: 52 additions & 22 deletions DictionaryManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
public class DictionaryManagement extends Dictionary{

public Scanner Scan = new Scanner(System.in);

private static final String fileIN = "E://Dictionaries.txt";
public int binarySearch(String s)
{
int mid,fi = 0;
Expand Down Expand Up @@ -39,16 +39,32 @@ public void insertFromCommandline() {
}
}

public void insertFromFile() {
File file = new File("Dictionaries.txt");
try (Scanner sc = new Scanner(file)) {
while (sc.hasNext()) {
Word newWord = new Word();
newWord.setWord_target(sc.next().trim().replace("\ufeff", ""));
newWord.setWord_explain(sc.nextLine().trim());
Words.add(newWord);
// public void insertFromFile() {
// File file = new File("E:\\Dictionaries.txt");
// try (Scanner sc = new Scanner(file)) {
// while (sc.hasNext()) {
// Word newWord = new Word();
// newWord.setWord_target(sc.next().trim().replace("\ufeff", ""));
// newWord.setWord_explain(sc.nextLine().trim());
// Words.add(newWord);
// }
// } catch (Exception e) { }
// }
public void insertFromFile(){
try {
File file = new File(fileIN);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
/* Đọc từng dòng (line) dữ liệu.
Nếu đọc được null nghĩa là kết thúc */
while ((line = in.readLine()) != null){
Word w = new Word(line);
Words.add(w);
}
} catch (Exception e) { }
in.close();
}
catch (Exception e){
}
}

public void dictionaryLookup()
Expand Down Expand Up @@ -117,21 +133,35 @@ public void deleteWord() {
}
}

// public void dictionaryExportToFile() {
// try {
// FileOutputStream fos = new FileOutputStream("E:\\Dictionaries.txt");
// OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
// BufferedWriter bw = new BufferedWriter(osw);
// for (Word tu : Words) {
// String line = tu.getWord_target().trim() + " " + tu.getWord_explain().trim();
// bw.write(line);
// bw.newLine();
// }
// bw.close();
// osw.close();
// fos.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public void dictionaryExportToFile() {
BufferedWriter file = null;
try {
FileOutputStream fos = new FileOutputStream("Dictionaries.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
for (Word tu : Words) {
String line = tu.getWord_target().trim() + " " + tu.getWord_explain().trim();
bw.write(line);
bw.newLine();
file = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileIN), "UTF-8"));
for(int i = 0; i < Words.size(); i++) {
file.write(Words.get(i).getWord_target() + "\t" + Words.get(i).getWord_explain());
file.newLine();
}
bw.close();
osw.close();
fos.close();
} catch (Exception e) {
file.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
12 changes: 11 additions & 1 deletion Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ public class Word {

private String word_target, word_explain;

public Word(String s)
{
String [] a = s.split("\\s", 2);
this.word_target = a[0];
this.word_explain = a[1];
}

public Word() {
}

public String getWord_target() {
return word_target;
}
Expand All @@ -18,4 +28,4 @@ public void setWord_explain(String word_explain) {
this.word_explain = word_explain;
}

}
}

0 comments on commit d2f7b46

Please sign in to comment.