-1

I am trying to read the file dev.txt from the Raw folder

I have place the file:

enter image description here


Uri uri=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + CaringApp.getAppInstance().getPackageName() + "/raw/" + "dev.txt");
PEMParser pemParser = new PEMParser(new FileReader(uri.getPath()));

I am getting the exception:

java.io.FileNotFoundException: /com.appname.organizer/raw/dev: open failed: ENOENT (No such file or directory)

at line:

PEMParser pemParser = new PEMParser(new FileReader(uri.getPath()));
Devrath
  • 42,072
  • 54
  • 195
  • 297

4 Answers4

1

I am trying to read the file dev.txt from the Raw folder

That is a file on your development machine. It is not a file on the Android device. FileReader will not work.

Instead, use openRawResource() on a Resources object to get an InputStream. If needed, wrap that in an InputStreamReader.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

In my app, I have used this and it is working for me :

URL url = getClass().getResource("/assets/dev.txt");
File f = new File(url.toURI());

Or you can directly get the input stream using getResourceAsStream() method :

InputStream  is= getClass().getResourceAsStream("/assets/levels.txt");
isr = new InputStreamReader(is);
0

Try to remove the extension to the filename. E.g. "dev" not "dev.txt" Like :

Uri uri=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + CaringApp.getAppInstance().getPackageName() + "/raw/" + "dev");
Nikunj
  • 3,937
  • 19
  • 33
0

I am using this function to copy a binary from raw to app files dir, and execute the binary from there.

    File dev = new File(getApplicationContext().getFilesDir(), "dev.txt");

        if (!dev.exists()) {
            try {
                dev.createNewFile();
            } catch (IOException e) {
                Log.e(TAG, "Failed to create new file!", e);
            }
        installBinaryFromRaw(this, R.raw.dev, dev);   

function to copy files from raw to /data/data//file/filename

    public static void installBinaryFromRaw(Context context, int resId, File file) {
        final InputStream rawStream = context.getResources().openRawResource(resId);
        final OutputStream binStream = getFileOutputStream(file);

        if (rawStream != null && binStream != null) {
            pipeStreams(rawStream, binStream);

            try {
                rawStream.close();
                binStream.close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close streams!", e);
            }

            doChmod(file, 755);
        }
    }
nkalra0123
  • 2,281
  • 16
  • 17