当前位置 : 主页 > 编程语言 > c++ >

打印信息到txt文件

来源:互联网 收集:自由互联 发布时间:2021-06-30
LogPrint.txt package com.xyy.lesson.timingpractice;import android.os.Environment;import java.io.File;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by lenovo on 2017/9/21. *//*用于存储
LogPrint.txt
package com.xyy.lesson.timingpractice;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lenovo on 2017/9/21.
 */

/*用于存储调试信息*/

public class LogPrint {

    private static LogPrint logPrint;

    private static boolean isPrintDebugInfo = true; //是否打印调试信息,true为打印,false为不打印

    //系统时间
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private String times = format.format(new Date(System.currentTimeMillis()));

    //文件存放目录
    public static final String ExternalSD_ROOT = Environment.getExternalStorageDirectory().toString(); //sd卡的根目录
    public static final String DebugLog_File_Dir = ExternalSD_ROOT+File.separator+"TimingPractice_Debug"+File.separator; //调试信息的文件夹目录
    public String Debug_File_Name = "Debug_" + "SELF" + ".txt"; //调试信息的文件名

    //调试信息的标签
    private static final String DebugLabel = "Debug: ";

    public LogPrint(){
        //构造函数,创建对象时会自动调用
		File filedir = new File(DebugLog_File_Dir);
            if (!filedir.exists()) {
                filedir.mkdir();
            }
    }

    public static LogPrint getInstance()
    {
        if(logPrint==null)
        {
            logPrint = new LogPrint();
        }
        return logPrint;
    }

    //类外部调用的方法
    public void setIsPrintDebugInfo(Boolean bool)
    {
        isPrintDebugInfo = bool;
    }

    public void saveLogInfoToSD(String SaveString)
    {
        if(isPrintDebugInfo)
        {     
            try
            {
                String timesinfo = format.format(new Date(System.currentTimeMillis()));
                File filename = new File(DebugLog_File_Dir+Debug_File_Name);
                if (!filename.exists()){filename.createNewFile();}
                FileOutputStream fileOutputStream = new FileOutputStream(filename,true);
                String DebugInfo = DebugLabel + timesinfo + "\n" + SaveString + "\n";
                fileOutputStream.write(DebugInfo.toString().getBytes());
                fileOutputStream.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    /*
    //类内部调用的方法 功能尚未实现
    public String fileNameTime(String time)  //记录调试信息文件的创建日期
    {
        if(timesTemp==null)
        {
            timesTemp = time;
        }
        return timesTemp;
    }*/

}
LogPrint.java
package com.xyy.lesson.timingpractice;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lenovo on 2017/9/21.
 */

/*用于存储调试信息*/

public class LogPrint {

    private static LogPrint logPrint;

    private static boolean isPrintDebugInfo = true; //是否打印调试信息,true为打印,false为不打印

    //系统时间
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private String times = format.format(new Date(System.currentTimeMillis()));

    //文件存放目录
    public static final String ExternalSD_ROOT = Environment.getExternalStorageDirectory().toString(); //sd卡的根目录
    public static final String DebugLog_File_Dir = ExternalSD_ROOT+File.separator+"TimingPractice_Debug"+File.separator; //调试信息的文件夹目录
    public String Debug_File_Name = "Debug_" + "SELF" + ".txt"; //调试信息的文件名

    //调试信息的标签
    private static final String DebugLabel = "Debug: ";

    public LogPrint(){
        //构造函数,创建对象时会自动调用
		File filedir = new File(DebugLog_File_Dir);
            if (!filedir.exists()) {
                filedir.mkdir();
            }
    }

    public static LogPrint getInstance()
    {
        if(logPrint==null)
        {
            logPrint = new LogPrint();
        }
        return logPrint;
    }

    //类外部调用的方法
    public void setIsPrintDebugInfo(Boolean bool)
    {
        isPrintDebugInfo = bool;
    }

    public void saveLogInfoToSD(String SaveString)
    {
        if(isPrintDebugInfo)
        {     
            try
            {
                String timesinfo = format.format(new Date(System.currentTimeMillis()));
                File filename = new File(DebugLog_File_Dir+Debug_File_Name);
                if (!filename.exists()){filename.createNewFile();}
                FileOutputStream fileOutputStream = new FileOutputStream(filename,true);
                String DebugInfo = DebugLabel + timesinfo + "\n" + SaveString + "\n";
                fileOutputStream.write(DebugInfo.toString().getBytes());
                fileOutputStream.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    /*
    //类内部调用的方法 功能尚未实现
    public String fileNameTime(String time)  //记录调试信息文件的创建日期
    {
        if(timesTemp==null)
        {
            timesTemp = time;
        }
        return timesTemp;
    }*/

}
网友评论