Write to text file without overwriting in Java

前端 未结 9 1374
失恋的感觉
失恋的感觉 2020-11-30 05:02

I am trying to write a method that makes a \"log.txt file\" if one does not already exist and then writes to the file. The problem that I am encountering is every time I cal

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 05:37

    You can even use FileOutputStream to get what you need. This is how it can be done,

    File file = new File(Environment.getExternalStorageDirectory(), "abc.txt");
    FileOutputStream fOut = new FileOutputStream(file, true);
    OutputStreamWriter osw = new OutputStreamWriter(fOut);
    osw.write("whatever you need to write");
    osw.flush();
    osw.close();
    

提交回复
热议问题