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();
}
}

posted @ 2021-11-10 16:35  ZikC  阅读(811)  评论(0编辑  收藏  举报