Is there any one who know how to append text file....
# getting-started
t
Is there any one who know how to append text file. I m making access log file. My code only write once to a file, how to append text?
Copy code
class AccessLog: Serializable {
    companion object {
        var LOG = "/usr/local/logs/access_log"
        @Synchronized
        fun write(domain: String, remoteIp: String, userId: Int) {
            File(LOG).bufferedWriter().use { out ->
                out.write("""domain: $domain    remote_ip: $remoteIp    user_id: $userId    """)
            }
        }
    }
d
Copy code
FileOutputStream(File("foo"), true).bufferedWriter().use { writer ->
        
    }
👍 1
t
thanks @diesieben07
d
However you should probably use a logging library, they do this sort of thing much more efficient and less error prone 😉
👆 2
d
You can also just do
file.appendText("access\\n")
👍 1