不知为什么我读不到我要的文件,代码如下;请大侠指教
package com;import java.io.*;class InputFile {
private BufferedReader in;
public InputFile(String fname) throws Exception{
try{
in = new BufferedReader(new FileReader(fname));
}catch(FileNotFoundException e){
System.err.println("Could not open "+fname);
throw e;
//throw e.fillInStackTrace();
}catch(Exception e){
try{
in.close();
}catch(IOException e2){
System.err.println("in.close() unsuccessful");
}
throw e;
}finally{

}
}
public String getLine(){
String s;
try{
s = in.readLine();
}catch(IOException e){
throw new RuntimeException("readLine() failed");
}
return s;
}
public void dispose(){
try{
in.close();
System.out.println("dispose() successful");
}catch(IOException e2){
throw new RuntimeException("in.close() failed");
}
}
}public class Cleanup {

public static void main(String[] args) {
// TODO 自动生成方法存根
try{
   InputFile in = new InputFile("Cleanup.java");
String s;
int i = 1;
while((s = in.getLine()) != null){
i++;
}
   System.out.println("Cleanup.java line count: "+i);
in.dispose();
}catch(Exception e){
  System.err.println("Caught Exception in main");
e.printStackTrace();
}
}}

解决方案 »

  1.   

    Cleanup.java
    存在吗?你用绝对路径看看还有,出什么错了
      

  2.   

    Cleanup.java你给的就是一个字符串也不是文件名啊。
    建议最好用File f=new File("你的文件名")
    这样估计就没什么问题了,如果还有问题请发出编译错误报告。
      

  3.   

    说找不到文件,但文件在的,我在这个包目录下面新建read.txt读取仍然不可以
      

  4.   

    改为
    public InputFile(String fname) throws Exception{
    try{
    File f = new File(fname);
    in = new BufferedReader(new FileReader(f));
    }catch(FileNotFoundException e){
    System.err.println("Could not open "+fname);
    throw e;
    //throw e.fillInStackTrace();
    }
    报告
    Could not open Switch.java
    Caught Exception in main
    java.io.FileNotFoundException: Switch.java (系统找不到指定的文件。)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.io.FileReader.<init>(FileReader.java:55)
    at exception.InputFile.<init>(Cleanup.java:10)
    at exception.Cleanup.main(Cleanup.java:50)
    文件Switch.java就在Cleanup.java相同的目录下
      

  5.   

    public class Cleanup 缺少 package com; 不知道是不是你忘加了另外如果您用的是eclipse等开发工具的话,那么记得路径设置的正确
    Cleanup.java应该是....\com\Cleanup.java,因此,您应该写成
           InputFile in = new InputFile("src/com/Cleanup.java");
           InputFile in = new InputFile("com/Cleanup.java");
    加不加src看您是选择了“创建单独的源文件夹和输出文件”,还是选择了“使用项目文件作为源文件和类文件的根目录”如果以上所述仍不能解决您的问题,可以试试
    package com;import java.io.File;public class Cleanup {

    public static void main(String[] args) {
                 // 看看当前目录是哪里哈,相当于pwd
                 System.out.println(new File("").getAbsolutePath());
    }
    }然后再考虑 new InputFile(".../com/Cleanup.java");中所使用的路径
             
                          gooooooooooooooooood luck
      

  6.   

    你搞错了编译以后真正致信的是.class文件,而不是java文件,而对于.class文件来说,那个.java文件不在同一目录下