第1个回答 推荐于2016-09-17
/**
* 写文件
* @param to (File) 保存到的文件
* @param str (String) 字符串
* @param charset (String) 字符集编辑
* @param outNull (boolean) false: null值作为""处理
* @return
*/
public static boolean writeFile(File to, String str, String charset, boolean outNull){
if(to == null) return false;
if(charset == null || charset.length() == 0) charset = "UTF-8";
BuffWriter bw = null;
try {
insureDir(to.getParentFile());
bw = new BuffWriter(new OutputStreamWriter(new FileOutputStream(to), charset));
bw.setOutNull(outNull);
bw.append(str);
bw.flush();
bw.close();
bw = null;
return true;
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bw != null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}