/**
* 写文件
* @param filePath 写入的文件路径
* @param str 需要写入的数据
* @param append 是否追加到文件末尾
*/
public static void writeToFile(String filePath, String str, boolean append){
if(!CommonUtils.isEmpty(filePath)){//判空
try{
File file = new File(filePath);
if(file.isDirectory()){
logger.info("无法写入目录,请指定文件名");
return;
}
if(!file.exists()){
file.createNewFile();//文件不存在,创建文件
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append)));
if(!CommonUtils.isEmpty(str)){
bw.write(str);//写操作
}
if(bw != null){
bw.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
向文件中写入字符串
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 出现在eclipse中无法向文件HDFS文件系统写入的问题,这将直接导致eclipse下编写的程序不能在hadoo...