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