简体   繁体   中英

Reading from a file using JAVA IO in android App

I have a file which an external JAR needs. I tried adding that file to assets and provided the path straight away. But that din't work and threw an error

java.io.FileNotFoundException: /assets/model/en2hi/tune/1/joshua.config: open failed: ENOENT (No such file or directory)

The line from external JAR trying to read the file is:

FileInputStream fis = (filename.equals("-")) ? new FileInputStream(FileDescriptor.in) : new FileInputStream(filename);

Please suggest on how this can to be handled. Is there any other way than keeping that application out of android app? Or how to allow an android app read with Java IO?

You can have you file in /assets directory of your project and access it using your AssetManager class:

AssetManager aMgr= context.getAssets();
InputStream ipStream = aMgr.open("filename.txt");

or you can have it /res/raw directory and use it using:

InputStream is = getResources().openRawResource(R.raw.test);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM