android 打印日志信息到sdcard
来源:互联网 发布:serina女装淘宝网网址 编辑:程序博客网 时间:2024/04/29 04:19
package com.netposa.ui;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import android.R.integer;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
public class WriteLogFile {
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();
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import android.R.integer;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
public class WriteLogFile {
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();
}
}
}
0 0
- android 打印日志信息到sdcard
- android 打印日志信息到sdcard
- 打印日志信息到文件
- Android 使用log4j 保存log日志信息至sdcard中
- ios 日志信息打印到文件中
- android SDCard存储信息
- Android中日志信息的打印方式
- android中Log打印日志相关信息
- android 开发输出日志打印信息
- android 开发输出日志打印信息
- android 错误日志打印到本地
- android 错误日志打印到本地
- 把log4j的日志信息打印到swing组件上
- 把log4j的日志信息打印到swing组件上
- 将程序运行信息打印到 系统日志
- 打印对战内存异常信息到日志中
- 打印java异常堆栈信息到日志文件
- 把log4j的日志信息打印到swing组件上
- 我的学习之旅(15)tty.c
- 修复ubuntu的grub的方法
- 基于Lambda架构的数据实时计算系统
- 公司守则
- Express Api 翻译[二] ——Request篇
- android 打印日志信息到sdcard
- 如何在Fragment中使用ViewPager,并实现导航随手势滑动效果
- mybatis实战教程,mybatis入门到精通
- HTTP消息的格式
- 聊聊分布式事务
- python + selenium 收藏
- OC学习笔记五---OC特有语法一
- Android 中 AlarmManager的使用
- MyEclipse中怎么在接口中的方法定位到实现类