问题
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