如何调试 windows 中的错误文件名路径,例如 FileReader()

How to debug wrong filename path in windows given to for example an FileReader()

问题描述

在此示例中,我需要为我的 FileReader() 提供正确的文件名路径,但在此过程中我总是遇到问题。我需要一种自己调试的方法 并过上幸福的生活。

Whosebug 提供了一些关于这个主题的帖子,但我认为这对我来说并不容易。它或多或少是在描述问题而不是解决问题。请参阅我支持此观点的尝试的第 2 点。

我试过的

  1. 首先尝试找到 Javadoc of FileReader,但字面上没有说明正确的路径或如何找到它。它甚至没有暗示系统差异。
  2. 在Whosebug上搜索,找到比较好的答案: Here is a well received answer of Stephen C which I find too hard to understand somehow, probably because he does not give examples and I am still left with many question. I also found a post about where java looks for files 这当然是一个非特定问题,但至少让我了解了如何自己调试它。即用System.out.println(System.getProperty("user.dir"))
  3. 尝试了很多不同的路径,都没有成功。 I even ended up with an error,我不知道是什么原因造成的,但我实际上只玩过 android 工作室中破坏我的 R 文件的路径。删除文件然后将它们添加回去解决了我的问题。烦人而且很奇怪。

我总是会面临的一些基本问题

  1. 文件路径中使用正斜杠还是反斜杠?
  2. 您需要包含文件扩展名吗?为什么不?它不是文件名的一部分吗?
  3. 为什么首先会有系统差异?我以为 Java 是平台无关的,都包含在程序的 jar 文件中。这让我很困惑。
  4. 如何避免搜索解决方案,只知道如何自己调试。我知道它找到它在默认情况下查找的路径的唯一方法是使用 System.out.println(System.getProperty("user.dir")) 为我打印出 / 直到让我毫无头绪。当我在维基百科上查找时,我得到

    By default, the system root folder for Microsoft Windows is C:/Windows. When I give the full path name it still does not work C:/....

一些我经常尝试的代码

    String str;
    String testingFileLocation = "/app/src/main/res/dictionary/english";
    System.out.println(System.getProperty("user.dir"));
    try {
        new FileReader(testingFileLocation);
    } catch (IOException e) {
        System.out.println("Wrong!");
    }

输出

04-20 21:47:17.959  32152-32152/--- I/System.out﹕ /
04-20 21:47:17.959    2556-2556/--- I/System.out﹕ Wrong!

stacktrace 只给出了这个,并没有让我更接近任何解决方案:

04-20 22:47:14.699  25934-25934/--- I/System.out﹕ java.io.FileNotFoundException: C:/Users/name/AndroidStudioProjects/projectPath: open failed: ENOENT (No such file or directory)

也许这看起来更像是一个咆哮,因为它过于完整,但这真的让我很沮丧,因为一定有一些我不明白的地方。希望大家帮帮忙

我猜您想在 Android 上保存一些内容,然后尝试这些选项 http://developer.android.com/guide/topics/data/data-storage.html。 Windows 或您用于开发的任何平台与文件在 Android 中的位置无关。