Android输出日志到指定文件夹

Android输出日志到指定文件夹,第1张

Android输出日志到指定文件

在实际工作时,有时需要将log日志输出到某个文件夹方便查找问题

public class LogUtil {
    private static final int LEVEL_FILE = 0x2;

    
    public static void writerLog(int logLevel, String msg) {
        if (LEVEL_FILE == logLevel) {
            //保存到的文件路径
            final String filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            FileWriter fileWriter;
            BufferedWriter bufferedWriter = null;

            try {
                //创建文件夹
                File dir = new File(filePath, "lowTemperatureTest");
                if (!dir.exists()) {
                    dir.mkdir();
                }
                //创建文件
                File file = new File(dir, "lowTemperature.txt");
                if (!file.exists()) {
                    file.createNewFile();
                }
                //写入日志文件
                fileWriter = new FileWriter(file, true);
                bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write( msg + "=======时间 :"+ getCurrentTime()+ "n");
                bufferedWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bufferedWriter != null) {
                    try {
                        bufferedWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        } else {
            Log.d("lowTemperature", msg+"");
        }


    }
    private static String getCurrentTime() {
        Calendar calendar = Calendar.getInstance();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return  sdf.format(calendar.getTime());
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5683009.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存