1. 程式人生 > >Java以UTF-8編碼讀寫檔案

Java以UTF-8編碼讀寫檔案

以utf-8編碼寫入檔案:

FileOutputStream fos = new FileOutputStream("test.txt");   
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");   
osw.write(FileContent);   
osw.flush(); 

以utf-8編碼讀取檔案:
FileInputStream fis = new FileInputStream("test.txt");   
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");   
BufferedReader br = new BufferedReader(isr);   
String line = null,FileContent = "";   
while ((line = br.readLine()) != null) {   
    FileContent += line;   
    FileContent += "\r\n";  
}