//如何向一个UTF-8或者其他编码方式的文档中添加新词
//本程序可以添加一个字符串或者添加一个集合
import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class AddNewWord { public static OutputStreamWriter osw=null; public static BufferedWriter bw=null; public static String newWord; public static String path; public static ArrayList<String> list; public void addNewWords(String newWord,String path) { try { osw=new OutputStreamWriter(new FileOutputStream(path,true),"UTF-8"); bw=new BufferedWriter(osw); //bw.newLine(); bw.append(newWord); bw.newLine(); //System.out.println("写入成功!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("写入失败"); } finally { if (bw!=null) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("流关闭失败"); } } } } public void addArraryWords(Collection<String> list,String path) { Iterator <String>it=list.iterator(); while (it.hasNext()) { try { osw=new OutputStreamWriter(new FileOutputStream(path,true),"UTF-8"); bw=new BufferedWriter(osw); //bw.newLine(); bw.append(it.next().toString()); bw.newLine() ; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("多个词语添加失败"); } finally { if (bw!=null) try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("流关闭失败"); } } } } }