RaffaD
RaffaD

Reputation: 361

read and write in a file android

I have next problems:

(1) How to check whether a file exists ? I do it this way in MainActivity onCreate

File f = new File("punteggio.txt");
if(f.exist())
   readFromFile();
else{
    writeToFile();
    readFromFile();
}

but it does not work because every time I open my application file does not exist.

(2) Another problem. In my first activity I write and read from the file without problems, while in the second activity when I read from the file the string is empty .

Main activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textScore = (TextView) findViewById(R.id.textRecord);
    File f = new File("punteggio.txt");
    if (!f.exists()) {
        Log.d(TAG, "Writeee");
        writeToFile();
    } else {
        readFromFile();
    }
}

private void readFromFile() {
    String ret = "";
    try {
        FileInputStream fis = openFileInput("punteggio.txt");
        byte[] buffer = new byte[(int) fis.getChannel().size()];
        fis.read(buffer);
        String str = "";
        for (byte b : buffer) str += (char) b;
        fis.close();
        Log.d(TAG, str);
        textScore.setText("Record: " + str);
        Log.i("STACKOVERFLOW", String.format("GOT: [%s]", str));
    } catch (IOException e) {
        Log.e("STACKOVERFLOW", e.getMessage(), e);
    }
}

public void startGame(View v) {
    Intent i = new Intent(MainActivity.this, GamePanel.class);
    startActivity(i);
}

private void writeToFile() {
    String string = "0";
    try {
        FileOutputStream fos = openFileOutput("punteggio.txt", Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }
}

This is the secondActivity

private int readFromFile() {
    String str = "";
    int i = 0;

    try {
        FileInputStream fis = openFileInput("punteggio.txt");
        byte[] buffer = new byte[(int) fis.getChannel().size()];
        fis.read(buffer);
        for (byte b : buffer) str += (char) b;
        i = Integer.parseInt(str);
        fis.close();
    } catch (IOException e) {
        Log.e("STACKOVERFLOW", e.getMessage(), e);
    }
    Log.d(TAG, "Stringa iiiii: " + i);
    return i;
}

The variable is empty, why? Can you help me? Thanks

Upvotes: 0

Views: 78

Answers (1)

Chim Ly
Chim Ly

Reputation: 159

answer for (1) - opening and writing to file depend on where the file is located, it could be in phone storage OR in SD Card. as such, when implementing reading files system, care should be taken considering the location of the file. This, sometime will cause the error, cannot locating the file. Alternatively, a better way of doing it would be using share preference.

sidenote : storing game score in a text file is vulnerable because user can directly edit the scores in the text file and alter the game result.

Upvotes: 1

Related Questions