OutputStreamWriter解決亂碼


.txt文件中添加內容,確保追加內容成功
File file;
OutputStreamWriter osw = null;
FileOutputStream fos = null;
try {
file =new File("C:\\Users\\Administrator\\Desktop\\test.txt");
fos = new FileOutputStream(file,true);//true是在末尾追加,不寫的話覆蓋原本內容
osw = new OutputStreamWriter(fos,"utf-8");
osw.append("我是你爸爸");
} catch (FileNotFoundException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}finally {
try {
osw.close();
fos.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}

.txt文件中添加內容,確保追加內容成功
File file;
OutputStreamWriter osw = null;
FileOutputStream fos = null;
try {
file =new File("C:\\Users\\Administrator\\Desktop\\test.txt");
fos = new FileOutputStream(file,true);//true是在末尾追加,不寫的話覆蓋原本內容
osw = new OutputStreamWriter(fos,"utf-8");
osw.append("我是你爸爸");
} catch (FileNotFoundException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}finally {
try {
osw.close();
fos.close();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM