0

I created A Buffered Reader, File Reader in my Android app and placed two text files inside raw folder. Now I have to pass a file path into File Reader, but what is the path of my file now?

BufferedReader reader = new BufferedReader(new FileReader("path"));

image link

2 Answers2

1

but what is the path of my file now?

You don't have to pass file path into FileReader for reading file, here you can check following code snippet.

InputStream inputStream = null;
                try {

                    inputStream = getResources().openRawResource(R.raw.hello_world);

                    byte[] reader = new byte[inputStream.available()];

                    while (inputStream.read(reader) != -1) {}

                    editField.setText(new String(reader));

                    editField.setSelection(editField.getText().length());

                } catch(IOException e) {

                    Log.e(LOG_APP_TAG, e.getMessage());

                } finally {

                    if (inputStream != null) {

                        try {

                            inputStream.close();

                        } catch (IOException e) {

                            Log.e(LOG_APP_TAG, e.getMessage());

                        }

                    }

                }

            }

Raw folder is for what in ANDROID

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.In above example R.raw.hello_world.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

Raw mostly used with media files.

Gru
  • 2,686
  • 21
  • 29
0

By default, you will be working in your present class path. So, you have to go back to the folder where the raw files is present and go inside it and access the files you want to.

file:///

will go back to your project directory, the one that contains all the folders. From there , you can access the raw folder.