Android将日志文件保存为隐藏文件

时间:2015-03-17 12:30:51

标签: android android-log

通过Windows资源管理器访问文件时出现问题。我的文件保存在非ro​​ot设备上。我只能在Android设备上使用嵌入式文件管理器安装来查看它们,但是使用Windows资源管理器查看它们会有问题。我插入并插入了我的设备,但它不能正常工作。这是我的代码:

public class LoggerUtil {

private File sdRoot;
private String dir;
private File workingDirectory;
private File log;
private Context applicationContext = MApp.getContext();

public LoggerUtil() {
}

@SuppressLint("SimpleDateFormat")
public File createNewLogFile() {
    String currentDateandTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String path = sdRoot.toString() + dir + "log" + currentDateandTime + ".txt";
    return new File(path);
}

@SuppressLint("SimpleDateFormat")
public void appendLog(String text) {

    sdRoot = Environment.getExternalStorageDirectory();
    dir = "/Android/data/" + applicationContext.getPackageName() + "/log/";

    workingDirectory = new File(sdRoot, dir);
    workingDirectory.mkdirs();
    log = createNewLogFile();

    if (ABC.isOK()) {
        File logFile = log;
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            String currentDateandTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            // BufferedWriter for performance, true to set append to file
            // flag
            BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
            buf.append(currentDateandTime + " " + text);
            buf.newLine();
            buf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

我的执行是:

 LoggerUtil loger = new LoggerUtil();
   loger.appendLog("TEST");

0 个答案:

没有答案