【问题标题】:Android File.exists() is always trueAndroid File.exists() 始终为真
【发布时间】:2015-09-28 11:03:15
【问题描述】:

我正在尝试使用此功能将文件从资产文件夹复制到设备文件夹:

public static void copyJSON(Context aContext) {
    AssetManager assetManager = aContext.getResources().getAssets();
    String[] pFiles = null;
    try {
        pFiles = assetManager.list("ConfigurationFiles");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    if (pFiles != null) for (String pJsonFileName : pFiles) {
        InputStream tIn = null;
        OutputStream tOut = null;
        try {
            tIn = assetManager.open("ConfigurationFiles" + File.separator + pJsonFileName);
            String[] pList = aContext.getFilesDir().list(); //just for test
            File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
            tOut = new FileOutputStream(pOutFile);
            if (pOutFile.exists()) {
                copyFile(tIn, tOut);
            }
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + pJsonFileName, e);
        } finally {
            if (tIn != null) {
                try {
                    tIn.close();
                } catch (IOException e) {
                    Log.e("tag", "Fail closing", e);
                }
            }
            if (tOut != null) {
                try {
                    tOut.close();
                } catch (IOException e) {
                    Log.e("tag", "Fail closing", e);
                }
            }
        }
    }
}

如果我删除应用程序并运行代码,变量 pList 是空的,但 pOutFile.exists() 总是返回 true !!。

我不想在每次打开我的应用程序时再次复制它们,我这样做是因为我的所有应用程序都使用 JSON 来浏览所有屏幕,所以如果我更改 BBDD 中的任何值,则发送 WS一个新的 JSON 文件和应用程序响应,例如不再需要一个按钮,所以当你第一次下载我的应用程序时,我会复制原始 JSON,然后如果你使用该应用程序并且如果你有互联网连接,你将下载一个新的JSON 文件,它比 Bundle 中的文件更准确,并且会被覆盖,这是因为据我所知,我无法更改 assets 文件夹中的文件。

我到处都读过,都说同样的用法:

File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);

然后问这个:

pOutFile.exists()

我不知道我做错了什么。

感谢您的所有帮助。

【问题讨论】:

  • “我到处都读过,都说同样的使用这个......然后要求这个......” - 除了那不是你正在做的事情。您正在这两个步骤之间做额外的工作。

标签: java android file-io


【解决方案1】:

这样说:

File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
if (pOutFile.exists()) {
  tOut = new FileOutputStream(pOutFile);
  copyFile(tIn, tOut);
}

一切都应该正常。请记住,FileOutputStream 创建了它应该流式传输到的文件(如果可能且不存在)

【讨论】:

  • 感谢我朋友对 FileOutputStream 的澄清,现在一切正常
【解决方案2】:

问题在于您实际上是在创建一个文件,然后检查它是否存在。

try {
    tIn = assetManager.open("ConfigurationFiles" + File.separator + pJsonFileName);
    String[] pList = aContext.getFilesDir().list(); //just for test
    File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);

    // See here: you're creating a file right here
    tOut = new FileOutputStream(pOutFile);

    // And that file will be created in the exact location of the file
    // you're trying to check:
    if (pOutFile.exists()) {  // Will always be true if FileOutputStream was successful
        copyFile(tIn, tOut);
    }
}

您应该在完成存在检查后创建 FileOutputStream。

来源:http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html

【讨论】:

    【解决方案3】:

    您刚刚创建的没有异常的文件始终存在。测试毫无意义。删除它。

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 2015-03-16
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 2015-12-05
      相关资源
      最近更新 更多