Search code examples
javaandroidandroid-studioio

Reading a textfile in android studio


Hi I'm currently having issues with reading in a file in android studio. The file I want to read in is just a simple text file called test. This is the path of the file C:\Users\John\Documents\MadLibs\app\src\main\res\raw\test.txt. Here's what I'm trying:

BufferedReader br = new BufferedReader(new FileReader(getResources().openRawResourceFd(R.raw.test)));

I'm relitively new to android studio and really don't understand how to read in files. I assumed its just like java however everything I've tried fails. Anyone have any ideas?


Solution

  • Reading a textfile in android studio

            FileInputStream fileInputStream=openFileInput("file.txt");
            InputStreamReader InputRead= new InputStreamReader(fileInputStream);
    
            char[] inputBuffer= new char[READ_BLOCK_SIZE];
            String s="";
            int charRead;
    
            while ((charRead=InputRead.read(inputBuffer))>0) {
                String rs=String.copyValueOf(inputBuffer,0,charRead);
                s +=rs;
            }
            InputRead.close();
            Log.d(TAG,s);