简单的-写入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();
}
}

你可能感兴趣的:(java-常用功能)