简单的日志保存工具类
public class LogUtil {
private final static Boolean ENABLE = true;
private final static Boolean NEED_SAVE = true;
private final static String PATH = Environment.getExternalStorageDirectory().getPath() + "/MyLog//";
public static void d(String tag, String info) {
if (ENABLE) {
Log.d(tag, info);
save(tag, info);
}
}
public static void e(String tag, String info) {
if (ENABLE) {
Log.e(tag, info);
save(tag, info);
}
}
public static void save(String tag, String info) {
if (!NEED_SAVE) {
return;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
makeRootDirectory(PATH);
String filename = Datetimeutil.date() + ".txt";
String filePath = PATH + filename;
String strContent = simpleDateFormat.format(System.currentTimeMillis()) + ":" + tag + ":" + info + "\r\n";
try {
File file = new File(filePath);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(strContent.getBytes());
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
定时删除工具类,删除30天以前的
public static void removeFileByTime(String dirPath) {
//获取目录下所有文件
List<File> allFile = getDirAllFile(new File(dirPath));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//获取当前时间
Date end = new Date(System.currentTimeMillis());
try {
end = dateFormat.parse(dateFormat.format(new Date(System.currentTimeMillis())));
} catch (Exception e){
e.printStackTrace();
}
for (File file : allFile) {//ComDef
try {
//文件时间减去当前时间
Date start = dateFormat.parse(dateFormat.format(new Date(file.lastModified())));
long diff = end.getTime() - start.getTime();//这样得到的差值是微秒级别
long days = diff / (1000 * 60 * 60 * 24);
if(30 <= days){
deleteFile2(file);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
//删除文件夹及文件夹下所有文件
public static void deleteFile2(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
deleteFile2(f);
}
file.delete();
} else if (file.exists()) {
file.delete();
}
}
//获取指定目录下一级文件
public static List<File> getDirAllFile(File file) {
List<File> fileList = new ArrayList<>();
File[] fileArray = file.listFiles();
if(fileArray == null)
return fileList;
for (File f : fileArray) {
fileList.add(f);
}
fileSortByTime(fileList);
return fileList;
}
//对文件进行时间排序
public static void fileSortByTime(List<File> fileList) {
Collections.sort(fileList, new Comparator<File>() {
public int compare(File p1, File p2) {
if (p1.lastModified() < p2.lastModified()) {
return -1;
}
return 1;
}
});
}
自定义Constants接口保存一些常用的常量
String INTENT_ALARM_LOG = "delete_log";
如果想要定时删除需要用到闹钟广播,每天上午十点删除如下,可以在MainActivity的onCreate()下调用下面的代码
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(Constants.INTENT_ALARM_LOG);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pi);
然后写一个广播接收类AlarmReceiver ,再静态注册
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Constants.INTENT_ALARM_LOG)) {
new Thread(new Runnable() {
@Override
public void run() {
FileUtils.removeFileByTime(LogUtils.PATH);
}
}).start();
}
}
}
静态注册
<receiver android:name=".view.receiver.AlarmReceiver">
<intent-filter>
<action android:name="delete_log"/>
</intent-filter>
</receiver>