[java]代码库
/**
* 向指定路径的文本中写入数据
*
* @param path
* 文本路径
* @param text
* 需要写入的内容
* @return true表示成功,false表示失败
*/
public static boolean writeToFileByPath(String path, String text) {
if (null == path || null == text) {
return false;
}
File file = new File(path);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(file), "gbk");
bw = new BufferedWriter(writer);
bw.write(text);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("写入文件错误");
return false;
} finally {
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}