博客首页 注册 建议与交流 排行榜 加入友情链接         宝宝相册的专门空间
推荐 投诉 搜索: 帮助

A Better Day

居善地,心善渊,与善仁,言善信,

正善治,事善能,动善时。

夫唯不争,故无尤……

Add to Google
playingfly.cublog.cn


Java对文件操作和web的相对路径问题的例子 存档

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

发表于: 2008-04-28 ,修改于: 2008-04-28 11:24,已浏览157次,有评论0条 推荐 投诉


网友评论

发表评论