0

I am using below code to write data in a file.

public static void writeDataToFile(final String fileName, final String fileContents) {
    try {
        File file = new File(Environment.getExternalStorageDirectory() + fileName);

        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file, true);
        writer.append(fileContents);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        LogUtility.logInfo(TAG, e.getMessage());
    }
}

Here FileWriter Constructor takes boolean that means it concatenates data to file every time to the last data. What I am trying to get is to have a file that has logs of my activities I am performing. And I am achieving via above mentioned code. but the problem is it is always concatenating logs to the last of data every time. What I want is to write new log on starting ever time.By this I will not have search file to the bottom for last log. It will be on start evyer time. Any help

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • Why not saving a timestamp when the app is opened and use the timestamp to create the file, e.g. app_log_timestamp.log and write to it. This way you'd have a new log file every time the app is opened, so you can browse the latest logs easily. Might not be what you want, tho. – Silviu Burcea May 30 '19 at 14:01
  • Yes I am doing the same. I am creating file with time stamp but every file is supposed to have huge logs... – Abdul Waheed May 30 '19 at 14:04
  • why not using a logging lib? https://stackoverflow.com/questions/2915150/log4j-support-in-android – Silviu Burcea May 31 '19 at 05:26

3 Answers3

0

If file does not exist, you have written code to create a new file. Likewise, if file exists, you can delete the file, and create new one

Before deleting old file, you can copy contents into a String, and add them to content that is to be written in file before writing into file.

StringBuilder contentToWrite = new StringBuilder();
contentToWrite.append(newContent);
if (!file.exists()) {
    file.createNewFile();
} else {
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String line = bufferedReader.readLine();
    StringBuilder sb = new StringBuilder();
    while (line != null) {
        sb.append(line).append("\n");
        line = bufferedReader.readLine();
    }
    contentToWrite.append("\n\n" + sb);
    file.delete();
    file.createNewFile();
}
FileWriter writer = new FileWriter(file, true);
writer.append(contentToWrite);
writer.flush();
writer.close();

PS: Don't forget to close FileReader and BufferedReader in a finally statement.

Caleb Hillary
  • 525
  • 6
  • 10
0

You can set the append flag to false in the FileWriter constructor. Then, use the write() function instead of the append() function

FileWriter writer = new FileWriter(file, false);
writer.write(fileContents);
Chase Farmer
  • 113
  • 9
  • This prepends the content - not what they want: `If the second argument is true, then bytes will be written to the end of the file rather than the beginning.` - see https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html#FileWriter-java.io.File-boolean- – mikeb May 30 '19 at 14:13
  • 1
    Yes, but it will also clear the file. OP can do something to check whether or not he wants to clear the file, but this seems like a valid suggestion for a way to simply clear the file IMO. – Chase Farmer May 30 '19 at 15:03
0

Why don't you remove the file if it exists:

    if (!file.exists()) {
        file.createNewFile();
    } else {
        file.delete()
        file.createNewFile();
    }
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • If i delete file...previous data will be lost as well. I want to keep previous data as well. And further want to append new data at the head rather tail – Abdul Waheed May 30 '19 at 17:45