gpt4 book ai didi

java - 将文件写入内部存储时崩溃

转载 作者:行者123 更新时间:2023-11-30 03:44:37 24 4
gpt4 key购买 nike

我正在开发一个应用程序,但在尝试将文件保存到内部存储器时遇到了问题。

该文件是一个 xml,它位于我项目的 res/raw 文件夹中。当应用程序启动时,它会检查文件是否存在。如果它不存在,它将文件从 res/raw 文件夹复制到内部存储中,否则它会检索文件中的一个字段以检查其版本,如果它是较新的版本,它会删除以前的文件并再次复制它.

好吧,问题是如果我有一个新版本并且我删除了以前的版本,当我要再次复制它时,它会抛出一个 NullPointerExceptcion 并且我找不到问题,但是我在这两种情况下使用相同的代码,我不知道如何解决。

这是我正在使用的代码:

    private void writeFileToInternalStorage() {  
FileOutputStream fos = null;
try {
InputStream fXmlFile = getResources().openRawResource(R.raw.partidos);
File file = getBaseContext().getFileStreamPath("file.xml");
if(file.exists()) {
String resourcesVersion= getFileVersion(fXmlFile);
String localVersion = getFileVersion(new FileInputStream(file));
if (!resourcesVersion.equalsIgnoreCase(localVersion)) {
//file.delete();
fos = openFileOutput("file.xml", Context.MODE_PRIVATE);
copyFile(fos, fXmlFile);
}
}
else {
fos = openFileOutput("file.xml", Context.MODE_PRIVATE);
copyFile(fos, fXmlFile);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

protected void copyFile(OutputStream fos, InputStream fXmlFile) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = fXmlFile.read(buffer)) > 0){
fos.write(buffer, 0, bytesRead);
}
}

protected String getFileVersion(InputStream file) {
String version = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
version = doc.getElementsByTagName("numero").item(0).getFirstChild().getNodeValue();
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return version;
}

有什么解决办法吗??

PS:异常抛出在这条语句上:

while((bytesRead = fXmlFile.read(buffer)) > 0) 

PS2:这是来自 LogCat 的代码:

03-03 20:47:07.140: W/System.err(2166): java.lang.NullPointerException: asset
03-03 20:47:07.148: W/System.err(2166): at android.content.res.AssetManager.readAsset(Native Method)
03-03 20:47:07.158: W/System.err(2166): at android.content.res.AssetManager.access$700(AssetManager.java:35)
03-03 20:47:07.168: W/System.err(2166): at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:573)
03-03 20:47:07.168: W/System.err(2166): at com.jfma75.myapp.MyApp.copyFile(MyApp.java:291)
03-03 20:47:07.178: W/System.err(2166): at com.jfma75.myapp.MyApp.writeFileToInternalStorage(MyApp.java:263)
03-03 20:47:07.178: W/System.err(2166): at com.jfma75.myapp.MyApp.onCreate(MyApp.java:62)
03-03 20:47:07.188: W/System.err(2166): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
03-03 20:47:07.188: W/System.err(2166): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
03-03 20:47:07.198: W/System.err(2166): at android.app.ActivityThread.access$1300(ActivityThread.java:130)
03-03 20:47:07.198: W/System.err(2166): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
03-03 20:47:07.208: W/System.err(2166): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 20:47:07.208: W/System.err(2166): at android.os.Looper.loop(Looper.java:137)
03-03 20:47:07.218: W/System.err(2166): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 20:47:07.218: W/System.err(2166): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 20:47:07.228: W/System.err(2166): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 20:47:07.237: W/System.err(2166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 20:47:07.237: W/System.err(2166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 20:47:07.248: W/System.err(2166): at dalvik.system.NativeStart.main(Native Method)

最佳答案

刚刚添加了我的评论作为答案,因为它似乎解决了 OP 的问题并将关闭此问题:

I think I know what the problem is. You open the resource file and hold it in an InputStream then you call String resourcesVersion=
getFileVersion(fXmlFile);
which, I guess, is your own private function to get the version. I'm betting you advance the InputStream or close it in that function. Then, when you later call copyFile the InputStream is either consumed or closed. Try just removing that line and put a constant String resourcesVersion = "versionThatDoesn'tMatch"; just to test the InputStream

关于java - 将文件写入内部存储时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15189811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com