Bad path trying to open file in Android

2019-08-16 00:07发布

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.

3条回答
走好不送
2楼-- · 2019-08-16 00:35

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

查看更多
Ridiculous、
3楼-- · 2019-08-16 00:56

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

查看更多
何必那么认真
4楼-- · 2019-08-16 00:59

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()

查看更多
登录 后发表回答