0

I am trying to write a file to internal memory in my android application, however, although this code is called, later when I try to read the file the app crashes with the fileNotFound exception. I am attempting to write an array, with each reference on a new line.

try{
            File myOutputFile = new File ("data/data/com.example.referencetool/files", filename );
            FileWriter fw = new FileWriter(myOutputFile, true);
            BufferedWriter bw = new BufferedWriter(fw);

            if(isFirstTime){
                myOutputFile.createNewFile();
            }

            int i = 0;

            while(i<=toSave.length){
                if(toSave[i].equals(null)){
                    bw.write("null");
                }
                else{
                    bw.write(toSave[i]);
                }
                bw.newLine();
                i++;
            }

        bw.flush(); 
        bw.close();

        }
        catch(Exception e){
            e.printStackTrace();
        }

Thanks!

jackgerrits
  • 791
  • 2
  • 8
  • 20

1 Answers1

0

You can Read/ Write your File in data/data/package_name/files Folder by,

 BufferedReader bufferedReader = new BufferedReader(new FileReader(new 
                           File(getFilesDir()+File.separator+"MyFile.txt")));
 String read;
 StringBuilder builder = new StringBuilder("");

 while((read = bufferedReader.readLine()) != null){
        builder.append(read);
      }
 Log.d("Output", builder.toString());
 bufferedReader.close();

check this link and link

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177