如何发送输出println()到System.err.我想使用字符串模板.
val i = 3
println("my number is $i")
Run Code Online (Sandbox Code Playgroud)
println()将消息发送到stdout,看起来没有选项发送到stderr.
你可以像在Java中那样做:
System.err.println("hello stderr")
Run Code Online (Sandbox Code Playgroud)
标准的stdout输出只是通过Kotlin中的一些辅助方法得到特殊的较短版本,因为它是最常用的输出.你也可以使用完整的System.out.println表格.
为什么不创建一个全局函数
fun printErr(errorMsg:String){
System.err.println(errorMsg)
}
Run Code Online (Sandbox Code Playgroud)
然后从任何软件调用它
printErr("custom error with ${your.custom.error}")
Run Code Online (Sandbox Code Playgroud)
不幸的是,科特林没有提供无处不在的写信方式stderr。
如果您将Kotlin用作JVM,则可以使用与Java中相同的API。
System.err.println("Hello standard error!")
Run Code Online (Sandbox Code Playgroud)
如果使用Kotlin Native,则可以使用打开stderr的功能,并使用platform.posix软件包手动对其进行写入。
val STDERR = platform.posix.fdopen(2, "w")
fun printErr(message: String) {
fprintf(STDERR, message + "\n")
fflush(STDERR)
}
printErr("Hello standard error!")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4797 次 |
| 最近记录: |