繁体   English   中英

java.io.FileNotFoundException错误

[英]java.io.FileNotFoundException error

我正在尝试读取文件并通过下面的代码对其进行序列化。 但是,我不断收到找不到文件错误。 我尝试将文件放在桌面上的文件夹中,并在eclipse本身中创建一个新文件。 请帮忙。

码:

public class Util implements Serializable {

    public static void main (String[] args) throws FileNotFoundException {
        try {
            Automobile ford = new Automobile();
            FileReader fr = new FileReader(
                        "C:/Users/FName LName/Desktop/Data.txt");
            BufferedReader br = new BufferedReader (fr);
            String line = new String ();

            FileOutputStream fos = new FileOutputStream ("car.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            FileInputStream fis = new FileInputStream ("car.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);

            int k=0;
            while ((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line);
                String s = new String ();
                if (st.hasMoreTokens()) {
                    s = st.nextToken();
                }

                OptionSet list = new OptionSet ();
                int j = 0;
                while (st.hasMoreTokens()) {
                    String temp = st.nextToken();
                    String name = new String ();
                    int price = 0;
                    StringTokenizer token = new StringTokenizer (temp);
                    while (token.hasMoreTokens()) {
                        name = token.nextToken(",");
                        price = Integer.parseInt(token.nextToken(","));
                        list.setOption(j,name,price);   
                    }
                    j++;
                }
                list.setName(s);
                ford.getOs().add(k, list);
                k++;
            }
            oos.writeObject(ford); 
            ford = (Automobile) ois.readObject(); 

            for(int i=0;i< ford.getOs().size();i++) {
                System.out.print(ford.getOs().get(i).getName()+":");
                for(int j=0;j<ford.getOs().get(i).getOpt().size();j++) {
                    System.out.print(ford.getOs().get(i).getOpt().get(j).getName() +
                            "," + ford.getOs().get(i).getOpt().get(j).getCost()+" ");
                }
                System.out.println();
            }

            ois.close();
            oos.flush();
            oos.close();
            br.close();
        } catch (IOException e) {
            System.out.println("File not found");
        } catch (ClassNotFoundException e) {
            System.out.println("File not found");
        }
    }
}

您可以重命名文件夹FName LName使其没有空格吗? 空格可能是个问题。 还可以尝试使用反斜杠(使用其他\\转义): C:\\\\Users\\\\FName LName\\\\Desktop\\\\Data.txt

还要检查您是否对该文件具有权限。 尝试将文件移动到C:\\然后看是否可读。

如果没有那个作品,你可以尝试检查JVM可以看到该文件:

File f = new File(filePathString);
if(f.exists()) { /* do something */ }

暂无
暂无

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

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