Java tutorial
//package com.java2s; import android.os.Environment; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; public class Main { public static void appendLog(String text) { File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file"); if (!logFile.exists()) { try { logFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { BufferedReader buf = new BufferedReader(new FileReader(logFile)); String line = ""; ArrayList<String> lines = new ArrayList<String>(); while ((line = buf.readLine()) != null) { lines.add(line); } int size = lines.size(); //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum int i = lines.size() - 500; if (i < 0) { i = 0; } buf.close(); BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true)); if (size > 1000) { bufW.write(""); for (; i < lines.size(); i++) { bufW.append(line); } } bufW.append(text + "\n"); bufW.close(); } catch (IOException e) { e.printStackTrace(); } } }