Kotlin – println使用字符串模板stderr
如何将println()
输出发送到System.err
。 我想使用字符串模板 。
val i = 3 println("my number is $i")
println()
发送消息到标准输出,它看起来像没有选项发送到标准错误。
你可以像在Java中那样做:
System.err.println("hello stderr")
标准stdout输出只是通过Kotlin中的一些辅助方法获得特殊的较短版本,因为它是最常用的输出。 您也可以使用完整的System.out.println
表单。
如果你想打印错误就像Kotlin中的Java一样,那么检查下面的代码:
System.err.println("Printing Error")
它将以红色打印。
但是,如果你只使用println()
那么它将工作如下:
System.out.println("Printing Hello")
为什么不创建一个全局函数
fun printErr(errorMsg:String){ System.err.println(errorMsg) }
然后从任何软件调用它
printErr("custom error with ${your.custom.error}")