词汇表处理脚本
将来写需要词库的游戏时用得到。
作用就是清理一下词汇表,把带连接线的带点的带大写字母的都干掉,然后把中文翻译也干掉,然后写出到文件,一行一个单词。
1 import java.io.*; 2 import java.util.ArrayList; 3 4 public class VocabularyProcesser { 5 File file; 6 ArrayList<String> list = new ArrayList<String>(); 7 8 VocabularyProcesser() {this.file = new File("vocabulary.txt");} 9 VocabularyProcesser(String file) {this.file = new File(file);} 10 VocabularyProcesser(File file) {this.file = file;} 11 12 public ArrayList<String> readVoc() { 13 try { 14 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 15 16 int asc; 17 char letter; 18 StringBuffer word = new StringBuffer(""); 19 20 while((asc = reader.read()) != -1) { 21 letter = (char) asc; 22 if (String.valueOf(letter).equals(" ")) { 23 list.add(word.toString()); 24 word.delete(0, word.length()); 25 reader.readLine(); 26 } else if (!String.valueOf(letter).matches("[a-z]")) { 27 word.delete(0, word.length()); 28 reader.readLine(); 29 } else { 30 word.append(letter); 31 } 32 } 33 34 reader.close(); 35 } 36 37 catch(IOException e) { 38 e.printStackTrace(); 39 } 40 41 return list; 42 } 43 44 public void writeVoc(ArrayList<String> list) { 45 File fileToWrite = new File("Voc.txt"); 46 47 try { 48 if (!fileToWrite.exists()) 49 fileToWrite.createNewFile(); 50 else 51 return; 52 53 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileToWrite))); 54 for (String i : list) { 55 writer.write(i); 56 writer.newLine(); 57 } 58 59 writer.flush(); 60 writer.close(); 61 } catch(IOException e) {} 62 } 63 }