Ad

File Handling: A New File Is Created Even After Using Append

So, I am trying to create a file in my Internal Storage, but the following code always make a new file and only stores the most recent string in the file. A part of the following code (try{}) is used twice. Can someone please tell me what am I doing wrong? In first case the message is sent by us and is transferred to listview and also being copied to a file as a backup, in the second case the message is being sent to us and is also transferred to listview, again making the string backup in the file.

File file = new File("/storage/emulated/0/BlueChat.txt");

private Handler handler = new Handler(new Handler.Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {

            case MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;

                String writeMessage = new String(writeBuf);
                chatMessages.add("Me: " + writeMessage);
                chatAdapter.notifyDataSetChanged();
                try
                {
                        if (!file.exists())
                        {
                            file.createNewFile();
                        }
                        else
                        {
                            FileWriter fw = new FileWriter(file.getAbsoluteFile());

                            fw.append("Me: " + writeMessage);
                            fw.close();
                        }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                break;
            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;

                String readMessage = new String(readBuf, 0, msg.arg1);
                chatMessages.add(connectingDevice.getName() + ":  " + readMessage);
                chatAdapter.notifyDataSetChanged();

                try
                {
                    if (!file.exists())
                    {
                        file.createNewFile();
                    }
                    else
                    {
                        FileWriter fw = new FileWriter(file.getAbsoluteFile());

                        fw.append(connectingDevice.getName() + ":  " + readMessage);
                        fw.close();
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                break;

        }
        return false;
    }
});
Ad

Answer

Instead of using

FileWriter fw = new FileWriter(file.getAbsoluteFile());

use

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

take a look at the second constructor of FileWriter class:

FileWriter(String fileName, boolean append)

Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

More info here: https://developer.android.com/reference/java/io/FileWriter#public-constructors

Ad
source: stackoverflow.com
Ad