自动生成文件到WEB-INF下,路径怎么写?怎么配置根路径?

blue小脚丫 2013-07-30 06:51:18
我写了一个方法,用来在有异常的时候自动生成日志文件并将异常信息记录在此文件中,现打算把这个日志文件放项目的WEB-INF下,路径要怎么写?
领导说要配置根路径,根路径是什么?怎么配置?求大神指教。

public void createRepairLog(String p_registNo, String p_verifylossNo) {
String details = "事故号:" + p_registNo + "定损单号:" + p_verifylossNo;
BufferedWriter out = null;
OutputStreamWriter osw = null;
FileOutputStream fos = null;
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String exDate = sdf.format(d);
/* 文件名 */
File file = new File("test.txt"); //路径要怎么写?
try {
file.createNewFile();
fos = new FileOutputStream(file, true);
if(fos==null){
}else{

}
osw = new OutputStreamWriter(fos);
out = new BufferedWriter(osw);
out.write(exDate + System.getProperty("line.separator"));
// 将String写入到文件中
out.write(details);
out.flush();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
fos.close();
osw.close();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
...全文
382 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
oh_Maxy 2013-07-31
  • 打赏
  • 举报
回复
引用 3 楼 daxia_DN 的回复:
[quote=引用 1 楼 oh_Maxy 的回复:] System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+"WEB-INF");
能说一下根路径是怎么一回事吗。。。跟项目的.classpath文件有关系吗[/quote] 按我这样写,就会构成一个 “工程目录/WEB-INF”的路径了。 不是根路径,和classpath没关系
blue小脚丫 2013-07-31
  • 打赏
  • 举报
回复
引用 1 楼 oh_Maxy 的回复:
System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+"WEB-INF");
能说一下根路径是怎么一回事吗。。。跟项目的.classpath文件有关系吗
ghostkngiht 2013-07-31
  • 打赏
  • 举报
回复
servlet的init函数中通过config.getServletContext().getRealPath("/")获取程序绝对路径。
oh_Maxy 2013-07-30
  • 打赏
  • 举报
回复
System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+"WEB-INF");

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧