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());
}
}