免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1526 | 回复: 0
打印 上一主题 下一主题

如何得到代码文件所在相对目录的路径的绝对路径? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-24 16:06 |只看该作者 |倒序浏览
如何得到代码相对目录的路径?
假设这个代码文件是test.java
放在myjsp中
       String path="F:\\myjsp\\";
           String FileName="regEx.txt";
           ReadLocalfile myLocalfile =new ReadLocalfile();
           regEx=myLocalfile.getFileContet(path,FileName);

如果我test.java放到其他目录下,也能用用,如何获得执行文件现对路径的绝对路径?

public class test
{
public static void main(String args[])
{
   ReadLocalfile myLocalfile =new ReadLocalfile();
   String path="F:\\myjsp\\";
   String FileName="regEx.txt";
   String Webtext=myLocalfile.getFileContet(path,FileName);
   System.out.println(Webtext);
}
}





/*
输入本地文件。

*/
import java.io.*;
public class ReadLocalfile{
     static public String getFileContet(String path,String FileName)
     {
          String totalStr="";
          File f=new File(path,FileName);

           if(f.exists()){//检查File.txt是否存在
              try{
                              FileReader fr=new FileReader(path+FileName);//建立FileReader对象,并实例化为fr
                 //对FileReader类生成的对象使用read()方法,可以从字符流中读取下一个字符。
                  BufferedReader br=new BufferedReader(fr);//建立BufferedReader对象,并实例化为br
                              String Line="";
                  //判断读取到的字符串是否不为空
                                  int i=0;
                  do{
                                  i++;
                  Line=br.readLine();//从文件中继续读取一行数据
                                  if(Line!=null)
                              totalStr=totalStr+Line;
                  }while(Line!=null);
                                  br.close();
                                  fr.close();
                  return totalStr;
            
                          }catch(IOException e){
                            System.out.println("读文件出错";
                   return null;
              }
                         finally{
                         
                         }

           }else{
             System.out.println("文件不存在!";//输出目前所在的目录路径
           }
                return null;
      }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP