0

I'm programming a android app and i want to save data in a txt File and overwrite old the old data.

The File should look like: file content

I want to overwrite certain lines after pressing Buttons. They'll call for instance

writeFileData("002",0); //write 002 in line 0

The Function looks like:

 public void writeFileData(String data, int line) {

    String buffer;
    BufferedReader br = null;
    FileOutputStream fos = null;
    String[] lines = new String[999];

    try {

        File file = new File(getFilesDir().getAbsolutePath() + File.separator + "data.txt");
        if(!file.exists())
        {
            try
            {
                if(file.createNewFile())
                {
                    Toast.makeText(BaseActivity.this, "File was created", Toast.LENGTH_LONG).show();
                }
            }
            catch(Exception ex)
            {
                Log.e("Error creating file", ex.getMessage());
            }

        }
        fos = new FileOutputStream(file, false);


        FileReader fr = new FileReader(file);
        br = new BufferedReader(fr);

        int i = 0;
        while (i < 8)
        {
            buffer = br.readLine();

            if (buffer != null)
            {
                lines[i] = buffer + "\n";
                i++;
            }
            else
            {
                break;
            }
        }

        lines[line] = data;

        for(i = 0; i < lines.length; i++)
        {
            fos.write(lines[i].getBytes());
        }

        try
        {
            fos.flush();
            fos.close();
        }
        catch(Exception ex)
        {
            Log.e("Error closing/flushing", ex.getMessage());
        }
    }
    catch(Exception ex)
    {
        Log.e("Error creating streams", ex.getMessage());
    }

}

I placed the call inside the onCreate of my launcher-activity but logcat telling me: Error creating streams after starting my app.

I tried several other Streams but nothing is working.

A solution would be awesome. Thanks in advance.

Xaver Seiringer

EDIT

My logcat is showing:

E/Error creating streams: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference

Although my file was created for sure and I tried to write 111 in every line --> There should be something to read for br.readLine()

But actually the error message says that there is nothing inside the file.

EDIT2

I set buffer = "xxx" and let the programm toast the content of line 4 and it said xxx, but the moment i changed buffer = "xxx" too buffer = br.readLine the error started popping up again

EDIT3

I changed a part of the code to:

 while(i < 8)
        {
            buffer = br.readLine();

            if (buffer != null)
            {
                try {
                    lines[i] = buffer + "\n";
                    i++;
                }
                catch (Exception ex)
                {
                    Log.e("Error line = buffer", ex.getMessage());
                }
            }
            else
            {
                Log.e(TAG, "Nothing in Line");
                i++;
            }
        }

Now it says that my "Nothing in line" 8 times although im writing into the file after my app starts.

EDIT4

Problem 1 solved, but now this is popping up:

E/myLogs: Nothing in Line

Altough i call writeFullData in the launcher activity (Only when the app starts for the first time)

public void writeFullData() {

    FileOutputStream fos = null;

    try {

        File file = new File(getFilesDir().getAbsolutePath() + File.separator + "data.txt");
        if(!file.exists())
        {
            try
            {
                if(file.createNewFile())
                {
                    Toast.makeText(BaseActivity.this, "File was created", Toast.LENGTH_LONG).show();
                }
            }
            catch(Exception ex)
            {
                Log.e("Error creating file", ex.getMessage());
            }

        }
        fos = new FileOutputStream(file, false);


        for(int i = 0; i < 7; i++)
        {
            String write = "111";
            fos.write(write.getBytes());
        }

        try
        {
            fos.flush();
            fos.close();
        }
        catch(Exception ex)
        {
            Log.e("Error closing/flushing", ex.getMessage());
        }
    }
    catch(Exception ex)
    {
        Log.e("Error creating streams", ex.getMessage());
    }

}
  • 1
    Instead of just showing a `Toast`, use `Log.e()` to write the stack trace out to LogCat. You can then [examine that stack trace](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) to see where your problem lies. **Never** catch an unexpected exception without logging enough information for you to try to diagnose *why* you got that exception. – CommonsWare Mar 12 '16 at 15:03
  • @CommonsWare I forgot about that, thanks! I edited it now. – Xaver Seiringer Mar 12 '16 at 16:36
  • `String[] lines = new String[999];`. After that all lines[i] are null. From ii 0 to 999. You only read in 8 lines. So the rest is still null. Thsts why lines[i].getBytes() will give you that null pointer exception for i above 7. Cech for null before use. – greenapps Mar 12 '16 at 19:31
  • For the rest this is terrible code. You could much better read in line for line and append to a stringbuilder. Then if you have to change line five then do not add the old line but the new. After you read in the whole file in the Stringbuilder write it out. You will end up with much less code. – greenapps Mar 12 '16 at 19:35
  • Thanks! That helped a lot. – Xaver Seiringer Mar 13 '16 at 11:45
  • But now I get another error. :/ Nothing in line although i call writeFullData in the launcher Activity (as shown in Edit4) – Xaver Seiringer Mar 13 '16 at 12:11

0 Answers0