【问题标题】:Trying to load data from a binary file, results in FileNotFound but the file is located there if i search it in my windows search bar尝试从二进制文件加载数据,导致 FileNotFound 但如果我在 Windows 搜索栏中搜索该文件,则该文件位于那里
【发布时间】:2016-07-21 17:44:34
【问题描述】:

我一直在开发一个有趣的小程序,现在我注意到当我运行它并尝试加载数据时,它会说“文件名、目录名或卷标语法不正确”但是文件的位置完全一样

    @SuppressWarnings("resource")
public static void loadData(String filename) {
    int accounts = 0;
    File f;
    try {
    FileInputStream is = new FileInputStream("./Clients/accountstorage.MKREAD");
    ObjectInputStream ois = new ObjectInputStream(is);

    String lineSep = System.getProperty("line.separator");

    String nextLine = "";

    BufferedReader br = new BufferedReader(new FileReader("./Clients/" + "accountstorage.MKREAD"));
    while((nextLine = br.readLine()) != null) {
        f = new File((String) ois.readObject());
        System.out.println(f.isDirectory() + " " + f.isFile()  + " " + f.exists());
        System.out.println(f.getAbsolutePath());
        String path = f.getAbsolutePath();
        is = new FileInputStream(path.replace("\\.", "") + "/" + filename + ".ACC");
        ois = new ObjectInputStream(is);
        double bal = ois.readDouble();
        AccountState state = (AccountState) ois.readObject();
        String name = (String) ois.readObject();
        String lastname = (String) ois.readObject();
        int id = ois.readInt();
        Account accounte = new Account(id, name, lastname, bal, state);
        getStorage().addAccount(accounte);
        System.out.println("LOADED files");

        ois.close();
        is.close();
    }




    } catch(Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        Alert("NOTE", e.getMessage());
    }
}

这会导致以下结果:

    java.io.FileNotFoundException  C:\Users\lucas\workspace\Dinges\Clients\Ouwens\Lucas
\BalanceInfo.ACC (The filename, directory name, or volume label syntax is incorrect)
      at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at dinges.Account.loadData(Account.java:158)
        at dinges.Main.main(Main.java:14)

但是,当我在 Windows 栏中搜索它时 C:\Users\lucas\workspace\Dinges\Clients\Ouwens\Lucas 它出现了,BalanceInfo.ACC 在那里

但是当我用程序尝试它时它没有找到它,可能是什么问题?里面可能有无效字符吗?

【问题讨论】:

    标签: java binaryfiles java-io


    【解决方案1】:

    只需使用绝对文件路径。那将是更简单的解决方案。例如我会写这样的东西:

    String dirPath = "C:\Users\lucas\workspace\Dinges\Clients\Ouwens\Lucas\";
    

    并将它传递给我正在做的任何事情。与我的文件名连接将非常容易。

    如果你正在做的项目可以加载到不同的计算机上,这种方法是行不通的,但在这种情况下,你需要做的就是使用这个:

    System.getProperty("user.dir"));
    

    它将为您提供您正在处理的当前目录。然后,您可以使用斜杠转到文件的保存位置。

    【讨论】:

      【解决方案2】:

      我猜文件名中的某处存在无效字符。

      换行试试

         is = new FileInputStream(path.replace("\\.", "") + "/" + filename + ".ACC");
      

         String fullPath = path.replace("\\.", "") + "/" + filename + ".ACC";
         System.out.println(java.util.Arrays.toString(fullPath.toCharArray()));
         is = new FileInputStream(fullPath);
      

      这三行中的第二行应该打印出构成fullPath 中字符串的各个字符,非ASCII 字符显示为字符转义序列,例如\n\u200b

      【讨论】:

      • 它仍然给出同样的错误,事实上现在它的错误是 = new FileInputStream(fullPath);
      • @LucasOuwens :我不希望对您的代码进行上述更改以使其正常工作。您问了“其中是否存在可能的无效字符?”这个问题,我的回答可以帮助您找出答案。我添加的行打印了什么?
      • 好吧!好吧,文件本身中没有出现未知空间,所以我将重写方法
      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 2020-02-16
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多