android 打印日志信息到sdcard

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import android.util.Log;

public class WriteLogToSD {
	public static int i = 0;

	public static void SaveToSDcard(String filename, String content)
			throws Exception {

		try {

			// File file = new File("/sdcard/pemlog"+i+".txt");
			File file = new File(filename + i + ".txt");
			if (!file.exists()) {
				file.createNewFile();
			}

			Log.i("writetest", "logfile size:"
					+ logFileSize(filename + i + ".txt"));

			if (logFileSize(filename + i + ".txt") > 200) {
				if (TransToZip(filename + i)) {
					DelFile(filename + i + ".txt");
				}

				i++;
				SaveToSDcard(filename, content);
				return;
			}

			FileWriter fw = new FileWriter(file, true);

			String logStr = getCurrTime() + "\t" + content + "\n";
			fw.write(logStr);
			fw.flush();
			fw.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 获取当前时间
	private static String getCurrTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		String time = dateFormat.format(new java.util.Date(System
				.currentTimeMillis()));
		return time;
	}

	// 获取当前运行代码的类名、方法名、行号 ,通过java.lang.StackTraceElement类
	public static String getLineInfo() {
		StackTraceElement ste = new Throwable().getStackTrace()[1];
		return ste.getFileName() + " " + ste.getMethodName() + ": Line "
				+ ste.getLineNumber();
	}

	// 判断当前文件大小
	public static long logFileSize(String filename) {
		File file = new File(filename);
		if (file.exists()) {

			return file.length();
		}

		return 0;
	}

	// 将txt文件 转化为zip文件保存
	public static boolean TransToZip(String filename) {
		File file = new File(filename + ".txt");

		File zipFile = new File(filename + ".zip");

		// 定义输入文件流
		InputStream input = null;
		// 定义压缩输出流
		ZipOutputStream zipOut = null;

		try {
			input = new FileInputStream(file);
			zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
			zipOut.putNextEntry(new ZipEntry(file.getName()));

			// 设置注释
			zipOut.setComment("www.demo.com");
			int temp = 0;
			while ((temp = input.read()) != -1) {
				zipOut.write(temp);
			}
			input.close();
			zipOut.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

		return true;

	}

	// 如果成功将txt转为zip文件保存,则删除txt文件
	public static void DelFile(String filename) {
		File file = new File(filename);
		if (file.isFile() && file.exists()) {
			file.delete();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值