开发者

append to file android

hey there, i need to append to m开发者_如何转开发y file but it is not working, it keeps overwriting the file, can anyone please tell me what is wrong:

  public void generateNoteOnSD(String sBody){

        try
        {
            File root = new File(Environment.getExternalStorageDirectory(), "AdidasParticipants");

            if (!root.exists()) {
                root.mkdirs();

            }

            File gpxfile = new File(root, "participants.txt");

            BufferedWriter bW;

            bW = new BufferedWriter(new FileWriter(gpxfile));
            bW.write(sBody);
            bW.newLine();
            bW.flush();
            bW.close();
            //Toast.makeText(mContext, "Tus datos han sido guardados", Toast.LENGTH_SHORT).show();
        }
        catch(IOException e)
        {
             e.printStackTrace();
          //   importError = e.getMessage();
            // iError();
        }
       } 

Thanks in advance.


You can fix it by changing the line where you assign the BufferedWriter:

bW = new BufferedWriter(new FileWriter(gpxfile, true));

When you open a file using the FileWriter constructor that only takes in a File, it will overwrite what was previously in the file. Supplying the second parameter as true tells the FileWriter that you want to append to the end of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜