Jonathan
Jonathan

Reputation: 85

My App doesn't work (BufferedWriter)

I programmed a small demo App for Android 4.1 or higher which should write a String into a file with BufferedWriter when clicked the button and should read this String and set the text of the Button to this String. I made it extra simple to avoid problems. But it doesn't work!

Source Code:

`package com.goldenskies.jonathan.readwritedemo;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    String filepath = "/saves/clicker";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 public void write(View view) {
        try {
            File f = new File(filepath);
            BufferedWriter out = new BufferedWriter(new FileWriter(f));
            out.write("Test erfolgreich!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    } public void read(View view) {
        try {
            File f = new File(filepath);
            BufferedReader in = new BufferedReader(new FileReader(f));
            String S = in.readLine();
            Button b = findViewById(R.id.button);
            b.setText(S);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And my XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/status"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:onClick="write"
android:text="Write"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="32dp"
android:onClick="read"
android:text="Read"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

Any help is highly appreciated.

Upvotes: 0

Views: 166

Answers (1)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

Your file path is /saves/clicker. This has two problems:

  1. You are trying to access the root folder (/) directly. This requires a rooted device, and su command to access.

  2. Even if you were trying to do that, you would first need to create a new directory with the name saves.

There are two solutions here:

  1. You use the app's default storage directory. This can be done using the following code:

    String filepath = "saves";
    ....
    File f = new File(getFilesDir(), filepath);
    f.mkdir();
    f = new File(f, "clicker");
    filepath = f.getPath();
    ....
    
  2. You use the default external storage directory. This requires permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE defined in AndroidManifest.xml to access the external storage. The code would be:

    String filepath = "saves";
    ....
    File f = new File(Environment.getExternalStorageDirectory(), filepath);
    f.mkdir();
    f = new File(f, "clicker");
    filepath = f.getPath();
    ....
    

You can also check if the directory already exists, before issuing a call to mkdir(). The code would be something like:

if(!f.exists() || !f.isDirectory())
    f.mkdir();

Upvotes: 1

Related Questions