public class FileIn_Out {
public static void main(String args[]) {
String filename = "e://aa.text";
Reader reader = null;
try {
File f = new File(filename);
System.out.println(f);// 测试路径是否正确
char[] t = new char[40];
int b;
reader = new InputStreamReader(new FileInputStream(filename));
while ((b = reader.read(t)) != -1) {
for (int i = 0; i < b; i++) {
System.out.print(t[i]);
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
}
出现问题:
e:\aa.text
java.io.FileNotFoundException: e:\aa.text (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at ygs.xy.a.FileIn_Out.main(FileIn_Out.java:15)

解决方案 »

  1.   

    File f = new File(filename);
    System.out.println(f);// 测试路径是否正确
    char[] t = new char[40];
    int b;
    reader = new InputStreamReader(new FileInputStream(filename));
    你这里new的是什么?里面放的filenname
    你上面file f=new,...
    你下面那里用到了F这个路径?
      

  2.   

    zengteng0204520quan:你好,“你这里new的是什么?里面放的filenname
    你上面file f=new,...
    你下面那里用到了F这个路径?”这个测试了路径是对的“e:\aa.text
    ”,下面的代码报错,这个很是纠结~
      

  3.   

     File file = new File(target);
          if(file.isFile()){
          System.out.println("true");
          }
          try {
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(target)));
    try {
    System.out.println(in.read());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  4.   

    还真不知道text是什么类型的文件
      

  5.   

    e://aa.text是指e盘下一个叫做aa.text的文件夹而非文件
    楼主何以用isDirectory() 来检查一下
      

  6.   

       在windows目录下的路径应该是"E:/aa.txt",楼主的文件路径有问题....
    请改正以后再试一试
      

  7.   

    如果你的File是一个文件夹,那么将抛出filenotfoundException,根据你的描述,很显然错误就在这里,intputStream 必须是读取文件,而不是文件夹
      

  8.   

    需要做两个检查:
    1、是否存在
    2、是否是目录
    public class TestDemo02 {
    public static void main(String args[]) {
    String filename = "e://aa.text";
    Reader reader = null;
    try { File f = new File(filename);
    boolean isExit = f.exists();
    if (isExit) {
    boolean isFile = f.isDirectory();
    if (!isFile) {
    char[] t = new char[40];
    int b;
    reader = new InputStreamReader(
    new FileInputStream(filename));
    while ((b = reader.read(t)) != -1) {
    for (int i = 0; i < b; i++) {
    System.out.print(t[i]);
    }
    } }else{

    System.out.println("操作的是目录,不是文件");
    } }else{
    System.out.println("文件不存在");
    } } catch (Exception e1) {
    e1.printStackTrace();
    } finally {
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException e) { }
    }
    }
    }
    }