Android examples for App:Log
log To File
//package com.java2s; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.Date; public class Main { private static final int LOG_SIZE = 1024 * 512; private static String sLogPath; private static String slogFile = "Log.txt"; private static SimpleDateFormat sDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); public static void logToFile(String content) { Date nowtime = new Date(); String needWriteMessage = sDateFormat.format(nowtime) + ":" + content;/*from ww w.j a v a2 s. c om*/ try { File file = new File(sLogPath, slogFile); if (!file.exists()) { ensurePath(file); file.createNewFile(); } FileWriter filerWriter = new FileWriter(file, true); BufferedWriter bufWriter = new BufferedWriter(filerWriter); bufWriter.write(needWriteMessage); bufWriter.newLine(); bufWriter.close(); filerWriter.close(); FileInputStream fis = new FileInputStream(file); if (fis.available() > LOG_SIZE) { file.delete(); } fis.close(); } catch (Exception e) { e.printStackTrace(); } } public static synchronized boolean ensurePath(File file) { String filePath = file.getParent(); File dir = new File(filePath); if (!dir.exists()) { dir.mkdirs(); } return dir.exists(); } }