Write logs into log file or append logs into the txt file in Android.

try this:-

public static void appendLog(String text) {
    Log.e("appendLog", "appendLog call");

    File log = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/Your Folder Name/Log");

    if (!log.exists()) {
        log.mkdir();
    }

    File logFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/Your Folder Name/Log/Logs_file.txt");

    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            Log.e("appendLog", e.getMessage());
            e.printStackTrace();
        }
    }

    try {
        //BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.append(text);
        buf.newLine();
        buf.close();
    } catch (IOException e) {
        Log.e("appendLog", e.getMessage());
        e.printStackTrace();
    }
}

Call method:-

appendLog("12:23 PM , Error while getting files.")

------------------- End ---------------------

search by:-



Comments

Popular posts from this blog

Android Studio 3.5 or above usb device not detected in Android Studio or Unable to detect adb version, exit value: 0xc0000135. ADB depends on the Windows Universal C Runtime, which is usually installed by default via Windows Update. You may need to manually fetch and install the runtime package here: https://support.microsoft.com/en-ca/help/2999226/update-for-universal-c-runtime-in-windows

Lint found fatal errors while assembling a release target.

ViewModelProviders.of(this) is deprecated what to use instead ViewModelProviders.of(this) OR if you don't want to use ViewmodelFactory OR ViewModelProviders error.