Android 打印输出信息

为了以后便于查找

/**打印信息类*/
public class FileWrite {

    /**根目录*/
    private  static final String SDCARD_PATH = Environment.getExternalStorageDirectory().getPath();         
    private  static final String LOG_FILE_PATH = SDCARD_PATH + File.separator+"lou"+File.separator+"log.log";   
    private  static final String parentpath = SDCARD_PATH + File.separator+"lou";   
    private static final String mCharset = "utf-8";
    private static FileWrite fw = null;
    private static FileOutputStream mFos;
    private static File mLogFile;
    private static File parentFile;
    private FileWrite(){
        //判断文件是否存在
        createIfNotExist(LOG_FILE_PATH);
        try {
            //此处判断文件是否存在,若存在则会追加 否则创建文件
            mFos = new FileOutputStream(mLogFile, true);
//          mFos = new FileOutputStream(LOG_FILE_PATH, true);
        } catch (FileNotFoundException e) {
            Log.e("lou", "FileWrite FileNotFoundException");
            e.printStackTrace();
        }
    }
    /**创建文件 判断文件是否存在
     * @param path 文件路径*/
    private void createIfNotExist(String path) {
        parentFile = new File(parentpath);
        if(!parentFile.exists()){
            parentFile.mkdir();
        }
        mLogFile = new File(path);
        if(!mLogFile.exists()){
            try {
                mLogFile.createNewFile();
            } catch (IOException e) {
                Log.e("lou", "FileWrite.createIfNotExist createNewFile failed");
                e.printStackTrace();
            }
        }
    }
    public static FileWrite getInstance(){
        if(fw == null){
            fw = new FileWrite();
        }
        return fw;
    }

    /**
     * 此处是调用的接口
     * @param content 打印的内容*/
    public void WriteString(String content){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        content = sdf.format(date)+":"+content;
        byte[] data;
        try {
            data = content.getBytes(mCharset);
            writeByte(data);
        } catch (UnsupportedEncodingException e) {
            Log.e("lou", ""+e.getMessage());
            e.printStackTrace();
        }
    }

    /**写文件
     * @param data 数据*/
    private boolean writeByte(byte[] data) {
        try {
            mFos.write(data);
            mFos.write("\r\n".getBytes());
            mFos.flush();
            return true;
        } catch (IOException e) {
            Log.e("lou", "writeByte failed");
            e.printStackTrace();
        }
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值