繁体   English   中英

FileNotFoundException错误

[英]FileNotFoundException errors

对不起,标题不好,想不到更好的标题。

我目前在FileNotfoundException这个矛盾的问题中,通过命令file.getCanonicalPath()和使用FileInputStream方法来定位我的文件。 我得到FileNotFoundException

以下是我使用的代码:

File file = new File("members.s");
        System.out.println(file.getCanonicalPath());

        FileInputStream fileIn = new FileInputStream("C:\\Users\\users\\Documents\\NetBeansProjects\\CWA2\\members.s");
        ObjectInputStream in = new ObjectInputStream(fileIn);

        byte[] b=new byte[fileIn.available()];
        for(int i=0;i<b.length;i++){
            m.add(mem = (Member)in.readObject());
        }

这是我得到的输出和异常错误。

    C:\Users\users\Documents\NetBeansProjects\CWA2\members.s
java.io.FileNotFoundException: C:\Users\users\Documents\NetBeansProjects\CWA2\members.s (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at Demos.DeserializeDemo.main(DeserializeDemo.java:21)

所以我有点困惑。 file.getCanonicalPath()方法如何找到我要使用的文件,但是FileInputStream返回错误。 谁可以帮我这个事?

file.getCanonicalPath()只会返回"members.s"作为其路径,而不是完整路径。 getCaninicalPath()删除多余的. ..从路径名。

因为FileInputStreamFile作为其参数(也是字符串btw),而FileString作为参数。

File file = new File("members.s");
System.out.println(file.getCanonicalPath());

FileInputStream fileIn = new FileInputStream("C:\\Users\\users\\Documents\\NetBeansProjects\\CWA2\\members.s");

这应该是

File file = new File("C:\\Users\\users\\Documents\\NetBeansProjects\\CWA2\\members.s");
FileInputStream fileIn = new FileInputStream(file);

当具有指定路径名的文件不存在或该文件确实存在但由于某种原因而无法访问时(例如,当试图打开只读文件进行写入时),将引发此异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM