try{ //需要生成 .txt的数据文件 //FileWriter fw=new FileWriter(Messages.getString("VoteDiaochaAction.3"));
//建立FileWriter对象,并实例化fw FileWriter fw=null; try {
//column.txt fw = new FileWriter ("../webapps/vote/amcolumn/data/column.txt"); } catch (RuntimeException e) { e.printStackTrace(); } /* //Java文件路径问题 System.out.println( Thread.currentThread().getContextClassLoader().getResource("")); System.out.println(VoteDiaochaAction.class.getClassLoader().getResource("")); System.out.println(ClassLoader.getSystemResource("")); System.out.println(VoteDiaochaAction.class.getResource("")); System.out.println(VoteDiaochaAction.class.getResource("/")); System.out.println(new File("column2.txt").getAbsolutePath()); System.out.println(System.getProperty("user.dir")); */ //FileWriter fw=new FileWriter ("/webRoot/amcolumn/data/column.txt");
//将字符串写入文件
fw.write(data); fw.close(); } catch (Exception e) { e.printStackTrace(); }
|
Java对文件操作和web的相对路径问题的例子。
Tomcat:在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
获得文件真实路径
string file_real_path=request.getRealPath("mypath/filename");
通常使用request.getRealPath("/");
|
相关链接:
http://www.blogjava.net/ericwang/archive/2005/12/16/24301.aspx
阅读(5142) | 评论(0) | 转发(0) |