package com.jqyd.app; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import com.jqyd.pub.JqydDateUtil; import android.content.Context; import android.os.Environment; import android.util.Log; public class WriteFile { private static String logStr = "walktour"; private String fileName; private Context context; private static String currDay = null; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); public WriteFile(Context context,String fileName) { this.context = context; this.fileName = fileName; currDay = dateFormat.format(new Date()); } /** * 写入日志 * @param content * @return */ public boolean writeToFile(String content) { content = JqydDateUtil.getDateSecOne(new Date())+content +"\n"; boolean flag = true; String path = ""; //判断是写入到SD卡中还是内存中,优先SD卡 if ((!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED))) {//内存 path= context.getFilesDir().getAbsolutePath() + File.separator + logStr+ File.separator+ "jqgj_recordLog"; }else{//SD卡 path=Environment.getExternalStorageDirectory().getPath() + File.separator + logStr+ File.separator + "jqgj_recordLog"; } Log.i("RECORDLOG", "文件path:"+path); File dirfile = new File(path); if (!dirfile.exists()) { dirfile.mkdirs(); } File file = new File(path + File.separator + fileName + ".txt"); try { FileOutputStream fos = new FileOutputStream(file, true); byte[] ba = content.getBytes("utf-8"); content = new String(ba); fos.write(content.getBytes()); fos.flush(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } /** * 删除文件 */ public void deleteFile(int n){ ArrayList<File> delFileName = new ArrayList<File>(); String record_path = ""; String err_path = ""; //判断是写入到SD卡中还是内存中,优先SD卡 if ((!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED))) {//内存 record_path= context.getFilesDir().getAbsolutePath() + File.separator + logStr+ File.separator+ "jqgj_recordLog"; }else{//SD卡 record_path=Environment.getExternalStorageDirectory().getPath() + File.separator + logStr+ File.separator + "jqgj_recordLog"; } //判断是写入到SD卡中还是内存中,优先SD卡 if ((!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED))) {//内存 err_path= context.getFilesDir().getAbsolutePath() + File.separator + logStr+ File.separator+ "crash"; }else{//SD卡 err_path=Environment.getExternalStorageDirectory().getPath() + File.separator + logStr+ File.separator + "crash"; } try { File recorDirfile = new File(record_path); File errDirfile = new File(err_path); File file = null; File recordFileArr[] = recorDirfile.listFiles(); int df = 0; if(recordFileArr!=null){ for(int i=0;i<recordFileArr.length;i++){ file = recordFileArr[i]; String fileDate = dateFormat.format(new Date(file.lastModified())); System.out.println("文件名:"+file.getName()); //System.out.println("转换后的日期:"+fileDate); //判断文件和制定日期的关系,删除最近的五天之外的数据 long day = getQuot(fileDate,currDay); if(day>=n){ df++; delFileName.add(file); } } } System.out.println("删除的记录日志数:"+df); writeToFile("删除的记录日志数:"+df); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss"); String log = "操作日期:"+dateFormat.format(new Date())+"---删除的日志记录数:"+df; //错误日志 File errFileArr[] = errDirfile.listFiles(); if(errFileArr!=null){ df = 0; for(int i=0;i<errFileArr.length;i++){ file = errFileArr[i]; String fileDate = dateFormat.format(new Date(file.lastModified())); System.out.println("错误日志文件名:"+file.getName()); //System.out.println("错误日志转换后的日期:"+fileDate); //判断文件和制定日期的关系,删除最近的五天之外的数据 long day = getQuot(fileDate,currDay); if(day>=n){ df++; delFileName.add(file); } } System.out.println("需要删除的错误日志数:"+df); writeToFile("需要删除的错误日志数:"+df); } //执行删除操作 int del = 0; for(File f : delFileName){ del++; boolean dres = f.delete(); System.out.println("第"+del+"个文件,删除结果:"+dres); } log += ",删除的错误日志记录数:"+del; writeToFile(log); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); writeToFile("没有SD卡,无法获取或写入日志"); } } /** * 操作时间 */ public void recordOptTime(String tContent){ writeToFile(tContent); } /** * 计算相差天数 * @param time1 * @param time2 * @return */ public long getQuot(String time1, String time2){ long quot = 0; SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd"); try { Date date1 = ft.parse(time1); Date date2 = ft.parse(time2); quot = date2.getTime() - date1.getTime(); quot = quot / 1000 / 60 / 60 / 24; } catch (ParseException e) { e.printStackTrace(); } return quot; } }