输出文件到项目

来源:互联网 发布:无纸化会议软件定制 编辑:程序博客网 时间:2024/05/14 05:51
String path = request.getSession().getServletContext().getRealPath("/statics/log/out.txt"); 

File out = new File(path);
System.out.println(1);

   System.out.println(out.getAbsolutePath());


if (!out.exists()) {

try {
out.createNewFile();

} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(2);
FileWriter fw = null;
BufferedWriter write = null;
try {
fw = new FileWriter(out, true);
write = new BufferedWriter(fw);
write.write("fwejfioewfewiofi");
write.newLine();


write.flush();
System.out.println("写入成功");

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
      fw.close();
       write.close();
 
} catch (IOException e) {
e.printStackTrace();
}
}


}