日期:2014-05-17  浏览次数:20639 次

自动生成文件到WEB-INF下,路径怎么写?怎么配置根路径?
我写了一个方法,用来在有异常的时候自动生成日志文件并将异常信息记录在此文件中,现打算把这个日志文件放项目的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();
}
}
}
Java 文件 路径 根路径 记录异常

------解决方案--------------------
System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+"WEB-INF");
------解决方案--------------------
servlet的init函数中通过config.getServletContext().getRealPath("/")获取程序绝对路径。
------解决方案--------------------
引用:
Quote: 引用:

System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+"WEB-INF");

能说一下根路径是怎么一回事吗。。。跟项目的.classpath文件有关系吗

按我这样写,就会构成一个 “工程目录/WEB-INF”的路径了。