我做了一个FileTest.java,编译后的类文件FileTest.class是在D:\Documents and Settings\Administrator\workspace\lx\bin下,现在我要读一个文本文件1.txt,它和FileTest.class在同一个目录下。
我用
String test=FileTest.class.getResource("").toString();
String testtext=test+1.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(testtext)));
结果不行,请问如何读这个文本文件,注意不要用
String testtext="D:\\Documents and Settings\\Administrator\\workspace\\lx\\bin\\1.txt";
这种方法。
谢谢

解决方案 »

  1.   

    String test=FileTest.class.getResource("").getPath();
      

  2.   

    PS:String testtext=test+"1.txt";
    貌似少个引号--
      

  3.   

    原因是FileTest.class.getResource("").toString();取到的是
    file:D:\Documents and Settings\Administrator\workspace\lx\bin\
    而在FileInputStream中不识别这样的路径。
    如果一定要使用这种方法,推荐将file:删除掉。
    可以使用
        String testtext=test+"1.txt";
        testtext = testtext.substring(testtext.indexOf(":") + 1);
    试一试.
    PS:
    LZ代码的第二行貌似缺了个"
      

  4.   

    我做了一个FileTest.java,编译后的类文件FileTest.class是在D:\Documents and Settings\Administrator\workspace\lx\bin下,现在我要读一个文本文件1.txt,它和FileTest.class在同一个目录下。
    我用
    String test=FileTest.class.getResource("").toString();
    String testtext=test+"1.txt";
    BufferedReader br = new BufferedReader(new InputStreamReader(
    new FileInputStream(testtext)));
    结果不行,请问如何读这个文本文件,注意不要用
    String testtext="D:\\Documents and Settings\\Administrator\\workspace\\lx\\bin\\1.txt";
    这种方法。
    谢谢
      

  5.   

    我运行时总显示
    Exception in thread "main" java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)
    如何表示1.txt的路径呢,谢谢
      

  6.   


    String test=FileTest.class.getResource("").getPath();
    String testtext=test+"1.txt";
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(testtext)));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
      

  7.   

    String testtext=test+"1.txt";
    我代码中没有少引号,是我在帖子里输入时短下了。
      

  8.   

    getResource的返回值是URL,要获得URL的路径用的是getPath方法,而不是简单的toString。
    所以:
    String test=FileTest.class.getResource("").getPath();
      

  9.   

    运行时还是显示
    java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)
      

  10.   

    整个代码是这样的
    public class FileTest3 {
    public static void main(String[] args) throws Exception,IOException {
    // TODO Auto-generated method stub
    String test=FileTest3.class.getResource("").getPath();
            String testtext=test+"1.txt";
            testtext = testtext.substring(testtext.indexOf(":") + 1);
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(testtext)));
          String line = br.readLine();
          System.out.println(line);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    }
    }
      

  11.   

    你又加上testtext = testtext.substring(testtext.indexOf(":") + 1)了?testtext = testtext.substring(testtext.indexOf(":") + 1);是另外一种解决思路……
      

  12.   

    程序如下:
    public class FileTest3 {
    public static void main(String[] args) throws Exception,IOException {
    String test=FileTest3.class.getResource("").getPath();
            String testtext=test+"1.txt";     
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(testtext)));
          String line = br.readLine();
          System.out.println(line);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    }
    }
      

  13.   

    你打印下testtext的路径,看这个路径下面有你的1.txt文件么?
      

  14.   

    String test=FileTest3.class.getResource("").getPath();
    先打印出你的test看看路径嘛
      

  15.   

    利用反射,FileTest.class.getClassLoader().getResourceAsStream("1.txt");
    确保1。txt的路径和FileTest。class在同一个路径
      

  16.   

    返回一个InputStream流,然后就可以操作了
      

  17.   

    class.getResource("")这种方法得到的路径中空格会用%20代替,换种其他获取路径的方法
      

  18.   

    问题就出在这:
    Exception in thread "main" java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)%20是什么东西呀?有这样的路径吗?除了%20之外还会出现%别的什么东西吗?
    这是什么原因出现了%,这是URL编码的问题麻,这个路径已经被编码过了,所以要解码,你都没有解码就拿来用自然就有可能出现问题了。只要在程序中加一句:
    testtext = java.net.URLDecoder.decode(testtext, "UTF-8");
    问题解决。参考URL编码:
    http://baike.baidu.com/view/204662.htm
    http://www.cnblogs.com/xryyforver/archive/2010/08/20/1804480.html
      

  19.   

    java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt  明示的是路径编码错误,不支持中文,要不你全部改成英文路径
      

  20.   

    %20是空格     .....Documents and Settings     
      

  21.   

    运行时还是显示
    java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。) 
    说的很明白,就是空格,程序没有任何问题,就是编码问题,楼上就说来了了嘛,真是,是个好人
      

  22.   

    我做过一个小程序 打成一个jar包发布 参数放在和这个jar包同一个文件夹下的 xml 文件里 例如:这个xml名称是 a.xml
    那么我们在程序中要得到这个xml文件 路径 用如下代码 File file=new File("");
    String url=file.getAbsolutePath()+"/a.xml";
    System.out.println(url);不知道对你是否有帮助
      

  23.   

    其实就是空格的问题 文件好解决:
    String test= java.net.URLDecoder.decode(FileTest.class.getResource("").toString(),"UTF-8");
    绝对管用;
      

  24.   


    20楼说的方式是正确的。用classLoader ,是使用相对路径,而不是绝对路径。