美文网首页
android 日志写入文件

android 日志写入文件

作者: ae12 | 来源:发表于2023-02-14 14:19 被阅读0次

1.pad 三代
//保存到的文件路径
final String filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
保存路径是: 在pad点开资源管理器后 直接internal memory 下的 xsy

2.保存日志到文件代码记录:

    /**
     * @param msg 打印的日志消息
     */
    public static void writerLog(String msg) {
            //保存到的文件路径
            final String filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            FileWriter fw = null;
            BufferedWriter bw = null;

            try {
                //创建文件夹
                File dir = new File(filePath, "xsy");
                if (!dir.exists()) {
                    dir.mkdir();
                }
                //创建文件
                File file = new File(dir, "soundlevel.txt");
                if (!file.exists()) {
                    file.createNewFile();
                }
                //写入日志文件
                fw = new FileWriter(file, true);
                bw = new BufferedWriter(fw);
                Date now = new Date(System.currentTimeMillis());
                String format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);

                bw.write(format + "   " + msg + "\n");
                bw.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bw != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }


相关文章