1. 程式人生 > >簡單的-寫入txt文字,追加寫入與覆蓋寫入

簡單的-寫入txt文字,追加寫入與覆蓋寫入

/**

* Description:追加的寫入
* @param pathName
* @param content
* @author diaowj:2016-4-21
*/
public static void writeFile(String pathName,String content){
File file = new File(pathName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}

try {
FileWriter writer;
writer = new FileWriter(pathName, true);

writer.write(content);
    writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**

* Description:覆蓋的寫入
* @param pathName
* @param content
* @author diaowj:2016-4-21
*/
public static void writeDetailFile(String pathName,String content){
try {
File file = new File(pathName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
writer.write(content);
writer.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}