简体   繁体   English

java.io.FileNotFoundException错误

[英]java.io.FileNotFoundException error

I am trying to read a file and serialize it through the code below. 我正在尝试读取文件并通过下面的代码对其进行序列化。 However, I keep getting the file not found error. 但是,我不断收到找不到文件错误。 I have tried putting the file in the folder, on the desktop and creating a new file in eclipse itself. 我尝试将文件放在桌面上的文件夹中,并在eclipse本身中创建一个新文件。 Please help. 请帮忙。

Code: 码:

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");
        }
    }
}

Can you try renaming the folder FName LName so it has no spaces? 您可以重命名文件夹FName LName使其没有空格吗? Spaces may be an issue. 空格可能是个问题。 Also try it with backslashes (escaped with an additional \\ ): C:\\\\Users\\\\FName LName\\\\Desktop\\\\Data.txt 还可以尝试使用反斜杠(使用其他\\转义): C:\\\\Users\\\\FName LName\\\\Desktop\\\\Data.txt

Also check if you have permissions to that file. 还要检查您是否对该文件具有权限。 Try moving the file to the C:\\ and see if it's readable. 尝试将文件移动到C:\\然后看是否可读。

If none of that works, you can try to check if the JVM can see the file: 如果没有那个作品,你可以尝试检查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