Android Automator:是否可以在下载文件夹中附加文件?

发布于 2025-01-14 03:25:07 字数 1540 浏览 3 评论 0原文

我已阅读 Kotlin 附加到文件 线程,我的 UIAutomator 代码如下所示:

fun appendLog(text: String?) {
    val logFile = File("/sdcard/Download/autotest.log")
    if (!logFile.exists()) {
        try {
            logFile.createNewFile()
            Log.d(TAG, "Create new file $logFile Succeed!")
        } catch (e: IOException) {
            Log.e(TAG, "Create new file $logFile FAILED!, $e")
        }
    }
    try {
        FileOutputStream(logFile, true).bufferedWriter().use { writer ->
            writer.append(text)
            writer.newLine()
        }
    } catch (e: IOException) {
        Log.e(TAG, "Appending file $logFile FAILED!, $e")
    }
}

// test cas 1
appendLog("Some test result")

// test cas 2
appendLog("Other test results")

但从 adb 收到错误日志猫

03-14 20:16:57.829 27499 27522 D LogTest : Create new file /sdcard/Download/autotest.log Succeed!
03-14 20:16:57.830 27499 27522 E LogTest : Appending file /sdcard/Download/autotest.log FAILED!, java.io.FileNotFoundException: /sdcard/Download/autotest.log: open failed: EEXIST (File exists)

03-14 20:17:02.214 27499 27522 D LogTest : Create new file /sdcard/Download/autotest.log Succeed!
03-14 20:17:02.216 27499 27522 E LogTest : Appending file /sdcard/Download/autotest.log FAILED!, java.io.FileNotFoundException: /sdcard/Download/autotest.log: open failed: EEXIST (File exists)

I've read the Kotlin append to file thread, and my UIAutomator code looks like:

fun appendLog(text: String?) {
    val logFile = File("/sdcard/Download/autotest.log")
    if (!logFile.exists()) {
        try {
            logFile.createNewFile()
            Log.d(TAG, "Create new file $logFile Succeed!")
        } catch (e: IOException) {
            Log.e(TAG, "Create new file $logFile FAILED!, $e")
        }
    }
    try {
        FileOutputStream(logFile, true).bufferedWriter().use { writer ->
            writer.append(text)
            writer.newLine()
        }
    } catch (e: IOException) {
        Log.e(TAG, "Appending file $logFile FAILED!, $e")
    }
}

// test cas 1
appendLog("Some test result")

// test cas 2
appendLog("Other test results")

But got error from adb logcat

03-14 20:16:57.829 27499 27522 D LogTest : Create new file /sdcard/Download/autotest.log Succeed!
03-14 20:16:57.830 27499 27522 E LogTest : Appending file /sdcard/Download/autotest.log FAILED!, java.io.FileNotFoundException: /sdcard/Download/autotest.log: open failed: EEXIST (File exists)

03-14 20:17:02.214 27499 27522 D LogTest : Create new file /sdcard/Download/autotest.log Succeed!
03-14 20:17:02.216 27499 27522 E LogTest : Appending file /sdcard/Download/autotest.log FAILED!, java.io.FileNotFoundException: /sdcard/Download/autotest.log: open failed: EEXIST (File exists)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文