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();
}
}
}