import java.io.*;public class FileOutClasses {
public static void main(String[] args) throws Exception {
//  File file = new File("../../log4j.properties");//找不到文件
 InputStream is = new FileInputStream(
 "../../log4j.properties");//找不到文件
// InputStream is = FileOutClasses.class
// .getResourceAsStream("../../log4j.properties");
// 这里用getResouceAsStream可以用相对路径,而new File用相对路径却找不到文件,为什么呢?
}
}问题如下:
1. 文件log4j.properties放在classes目录外面时,只能用绝对路径访问,不能用相对路径。怎样才能使用相对路径呢?
2. 即时log4j.properties放在classes目录里面
   A.又出现下面两个问题:当new File时也不能用相对路径 ;
   B.要得到一个InputStream ,使用new FileInputStream()也不能用相对路径。但是用getResourceAsStream()却可以用相对路径,这是为什么呢?二者有何区别?
 * 

解决方案 »

  1.   

    getResourceAsStream()的相对路径是相对于.class文件的路径,而new FileInputStream()中的相对路径是相对于当前cmd中的路径,二者不一定相同
      

  2.   

    我在环境变量中有加上.; 这也就是说执行当前类的方法时, . 指向类所在的路径,也就相对于当前cmd中的路径,可是找不到文件,所以我认为 1楼的关于这点说法不对
      

  3.   


    String folder =FileOutClasses.class.getClassLoader().getResource("log4j.properties").toString();
    folder = folder.replace("%20"," "); 
    folder = folder.replace("file:/",""); 
    InputStream ips = new BufferedInputStream(new FileInputStream(folder));
      

  4.   

    那我想请问2楼的shine333,如果是把这个绝对路径硬编码到程序中,打成JAR包发布时就会有很大的限制——文件必须放到一致的路径下!
      

  5.   

    WEB项目,你的File的相对路径的根路径是你在项目里面看到的WebContent目录,一般是WEB-INF的上级。获取文件的方式:
    任意servlet里面:
    package com.comic.common;
    ..
    public class BackgroundServlet extends HttpServlet {
    ..
    public void init() throws ServletException {
    String fileName = this.getServletContext().getRealPath("/") + "WEB-INF/config/config.xml";
    File f = new File(fileName);
    ...一般的项目,你的File的相对路径的根路径是你项目的src目录,也就是编译后的classes目。
    假设你的配置在classes目录同级的一个config目录里面,则入口类的main函数里面:
    String fileName = "./config/user.txt";
    File f = new File(fileName);new File绝对可以用相对路径,关键是你的路径到底对不对?!
      

  6.   

    以你的代码来说,参见第二点,与src相同的建个config目录,就可以用 “./config/***.***”访问。import java.io.*;public class FileOutClasses {
        public static void main(String[] args) throws Exception {
    //         File file = new File("../../log4j.properties");//找不到文件
             InputStream is = new FileInputStream(
                     "../../log4j.properties");//找不到文件
    //        InputStream is = FileOutClasses.class
    //                .getResourceAsStream("../../log4j.properties");
            // 这里用getResouceAsStream可以用相对路径,而new File用相对路径却找不到文件,为什么呢?
        }
    }
      

  7.   

    试试以下两个方法,可以直接使用。
     public static String findLocalPath(Class clazz, String resourceName) {
            String originalPath = clazz.getResource(resourceName).toString();
            return prune(originalPath);
    }private static String prune(String source) {
            String prefix = "jar:";        if (source == null || source.isEmpty()) {
                return source;
            }
            StringBuilder buff = new StringBuilder(source);
            int index = 0;
            if (buff.indexOf(prefix) != -1) {
                buff.delete(0, prefix.length());//去除开头的"jar:"字串。
                index = buff.lastIndexOf(".jar");
                buff.delete(index, buff.length());//此时文件名一定是.jar结束,删除从.之后的所有字符。
            }
            //buff.delete(0, "file:".length());        index = buff.lastIndexOf("/");
            buff.delete(index, buff.length());//去掉文件名。
            return buff.toString();
    }
    这样使用,假定jar包下有个文件叫flag.public class Main {
      public static void main(String[] args) {
         String localPath = findLocalPath(Main.class,"/flag");
         System.out.println(localPath);
      }
    }这样就得到了到当前jar包所在的目录了。
    比如jar包路径是“c:\test\myjar.jar”,那么方法得到的是"c:/test/"这个路径。
    希望对你有用。
      

  8.   

    续10楼。这样如果你的外部配置文件放在"c:\test\config\"目录下,你就可以用这个得到的路径来组装出需要的绝对路径了。
      

  9.   

    先用getResource("/")获得你当前类的目录 再通过截取 获得你想要的路径再加上你的文件名 
      

  10.   

    张总,是这样的。您在家里的电脑上按Ctrl-C,再到公司里的电脑上按Ctrl-V不行的。即使同一篇文章也不行……不,不,再贵的电脑也不行这句话经典的
      

  11.   

    这是一个classload的问题,你必须找到那个文件相应的classload,java才会去处理classes文件以外的空间,也就是说即使你的class不是放在classer文件夹下,只要知道相应的classload,都可以处理。还有一种方法就是设在环境变量中
      

  12.   

    相对路径是相对于 System.getProperty("user.dir"); 系统属性值中的路径