[android]-记录日志到sd卡

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;


public void logToFile(String message) {
	SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");
	String datetimeToday = tempDate.format(new java.util.Date());
	String filepath = "/mnt/sdcard/logfolder/log_"
			+ datetimeToday + ".txt";
	//文件名如log_20120916.txt
	// -----------
	// String filepath="";
	File f = new File(filepath);
	try {
		FileWriter fw = new FileWriter(f, true);
		fw.write(message + "\n");
		fw.flush();
		fw.close();
	} catch (IOException e) {
		e.printStackTrace();
	}

}

实际使用中可根据实际需要更改或组合filepath值
此函数如日志文件存在,则在后面追加记录,不存在日志文件则创建

你可能感兴趣的:(android,log,日志)