Kewin Björk Nielsen Kewin Björk Nielsen - 4 months ago 12
Android Question

write to .txt and keep writing instead of replace

how do i getting it to make a new line and keep writing instead of replacing my text in the .txt and add a few other stuffs

1.5(this should only be added once in the top i am not sure how to do that)

LAGERIND(text)

KW89668(Cardnumberbox)

RHAL4 (shelfnumberbox)

thats how it should look like in the .txt

enter image description here

public void onPictureTaken(byte[] bytes, Camera camera) {
File dir = new File(path);
dir.mkdirs();
File file = new File (path + "/append.txt");
String [] saveText = String.valueOf("LAGERIND"+cardnumberbox.getText()+shelfnumberbox.getText()).split(System.getProperty("line.separator"));

cardnumberbox.setText("");

Save (file ,saveText);

public static void Save(File file, String[] data)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);
}
catch (FileNotFoundException e) {e.printStackTrace();}
try
{
try
{
for (int i = 0; i<data.length; i++)
{
fos.write(data[i].getBytes());
if (i < data.length-1)
{
fos.write("\n".getBytes());
}
}
}
catch (IOException e) {e.printStackTrace();}
}
finally
{
try
{
fos.close();
}
catch (IOException e) {e.printStackTrace();}
}
}

Answer
FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream(file, true);
    }
    catch (FileNotFoundException e) {e.printStackTrace();}

Using this constructor appends to the file FileOutputStream(file, true)

More details here (FileOutputStream api doc)

Comments