Bad path trying to open file in Android

孤街醉人 提交于 2019-12-24 07:04:53

问题


I'm trying to open a file with this:

document = builder.parse(new File("Data.xml"));

and I'm getting this message:

/Data.xml: open failed: ENOENT (No such file or directory)

and the file is in the root directory of the android project.


回答1:


You are trying to open a file located in / (in linux this is the root directory of your file system). Instead you should be trying to create a file either on the SDCard or within the local storage directory for your application.

See this for more clarification: http://developer.android.com/guide/topics/data/data-storage.html




回答2:


Move Data.xml into the assets folder of your project. Then to get a file reference, call getResources().getAssets().openFd( "Data.xml" )




回答3:


You should probably try using a file input stream constructor for the builder instead, and use openFileInput( String fileName ) to get that, which does only use your app's data directory.

Using persistent storage

openFileInput()



来源:https://stackoverflow.com/questions/10235888/bad-path-trying-to-open-file-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!