对一个文件持续的写入内容:
public static String createFile(String filePath , String content) {
File file = new File(filePath);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()) {
boolean hasFile = file.createNewFile();
if(hasFile){
System.out.println("file not exists, create new file");
}
fos = new FileOutputStream(file);
} else {
System.out.println("file exists");
fos = new FileOutputStream(file, true);
}
osw = new OutputStreamWriter(fos, "utf-8");
osw.write(content); //写入内容
osw.write("\r\n"); //换行
} catch (Exception e) {
e.printStackTrace();
}finally { //关闭流
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return filePath + "已创建完成";
}