Search code examples
androidbufferedreaderprintwriter

Android - Buffered reader and PrintWrite to save data


 public void loadFile(File infile) throws FileNotFoundException {
        InputStream inputStream = openFileInput(String.valueOf(infile));
        System.out.println("=====READING========");
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader sc = new BufferedReader(inputStreamReader);
            String temp = "";
                while ( (temp = sc.readLine()) != null ) {
                    System.out.println(sc.readLine());
        }
        inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

This is reader

public void saveGame(File outFile) throws FileNotFoundException {
    // Name + UserInfo + CVList+
    FileOutputStream os = openFileOutput(String.valueOf(outFile), Context.MODE_PRIVATE);
    PrintWriter printWriter = new PrintWriter (os); //set up a writer
    String content=userInfo.toString();
    content=content+ tempList.toString();//set up the data to be printed
    printWriter.println (content);//print it
    System.out.println(content);
    printWriter.flush();
    printWriter.close ();
}

This is writer.

I just got this code from googling whilte finding out ways to r/w .txt files.

The codes are working, but I am wondering since this is android version.

1. Where are the text files stored? I can see InputStreamReader and FileOutputStream is doing something about it, but what is it doing?

2. Do the text files get deleted when app gets updated?

  1. Is it possible to somehow get the list of textfiles written by PrintWriter and delete one of them? (Like deleting a selected savefile)

Solution

    1. Files are stored in private directory of your app. Only your app can read/write these files.
    2. No, it won't. It will stay there but will be removed when uninstalling or on "clearing data" from android app settings.
    3. You need to know the name of the file you created to read/write/delete it.