当然可以用相对路径,估计你的路径错了
你先看看当前路径 new File("").getAbsolutePath()

解决方案 »

  1.   

    getAbsolutePath() 得到包括文件名在内的整个路径。
    File f= new File("test.ini") 这样的结果是JVM去和文件夹com同级的目录下去找test.ini文件,而你的test.ini文件实际上是在..\com\jv01\这个路径下,所以会提示找不到文件
      

  2.   

    /**
              @(#)ReadFile.java
     */
    package com.quickpoint.ReadFile;
    import java.io.*;/**
              ReadFile 是一个读写文件的类
              
     */
    public class ReadFile {
       public static void main(String[] args) {
          String fileName = "test.txt";
          
          if( args.length > 1 ) {
             fileName = args[0];
          }
          
          
          System.out.println( ReadFile.readStringFromFile(fileName));
       }  
       
       public static String readStringFromFile(String fileName) {
         BufferedReader br = null;
         try {
           br = new BufferedReader( 
                                      new FileReader( 
                                            new File(fileName)));
           StringBuffer buf = new StringBuffer();    
           String tmpStr = null;
           while( (tmpStr = br.readLine()) != null ) {
              buf.append( tmpStr );
           }                       
         
          br.close();
          br = null;
          
          return buf.toString(); 
          } catch (FileNotFoundException ex) {
              ex.printStackTrace();
              return null;
          } catch (IOException ex) {
              ex.printStackTrace();
              return null;
          }  
          
          finally {
             if (null != br) {
                try {
                br.close();
                } catch(IOException ex) {
                   // do nothing
                }
             }   
          }     
       }
       
    }目录结构:
    test.txt
    com 
     |   quickpoint 
            | ReadFile
                 | ReadFile.java
    这个是采用相对路径的。
    你把要读的文件放到包的最外边,不是class的当前目录中就可以了。
      

  3.   

    test.ini放在你执行程序的路径下,就能找到了.
      

  4.   

    可以的,都放在同一个目录里边就可以了
    或者相对于project的相对路径../conf/test.ini