java.io.FileNotFoundException 错误

标签 java file-io error-handling

我正在尝试读取文件并通过下面的代码对其进行序列化。但是,我不断收到未找到文件的错误。我尝试将文件放在桌面上的文件夹中并在 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 */ }

关于java.io.FileNotFoundException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074395/

相关文章:

variables - 如何在 Powershell 中手动增加 $err 计数?

ajax - 从 AJAX 获取错误消息时出现问题 - 仅在电话上出错

authentication - 如何处理部署在 Kestrel 上的 asp.net core mvc 中的 windows 身份验证弹出窗口的取消按钮?

java - 文件未在 jar 文件内的 java 中打开

java - Eclipse 为可访问的代码(变体)提供死代码警告

java - 通过 PhaseListener 访问 Controller

java - 我如何编写一个 Java 代码,通过使用贪心算法设计来解决这个问题?

c - 如何循环打开和关闭文件

Java:读取文件输入内容并在找到某些线条图案序列时对其进行过滤

java - 如何在 Flux.subscribe 之后收集数据并将其作为 json 数组发送?