Android 打印输出信息

来源:互联网 发布:淘宝店铺618活动策划 编辑:程序博客网 时间:2024/05/24 01:48

为了以后便于查找

/**打印信息类*/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;    }}
原创粉丝点击