0

通过 Windows 资源管理器访问文件时出现问题。我的文件保存在非根设备上。我只能使用安装在 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");
4

0 回答 0