【发布时间】: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()
我不知道我做错了什么。
感谢您的所有帮助。
【问题讨论】:
-
“我到处都读过,都说同样的使用这个......然后要求这个......” - 除了那不是你正在做的事情。您正在这两个步骤之间做额外的工作。