2

我想格式化我的 java 程序的输出,以便每当它被重定向到一个文件(xl 或 csv 文件)时,我希望输出用逗号分隔,但在控制台上显示时它不应该用逗号分隔。这些应该在运行时。

4

1 回答 1

2

From Java 6 onwards we have the Console class. It's instance can be obtained by System.console() method. If no console device is available at runtime then invocation of this method will return null.

if(null == System.console()) {
    // write to file
    // code
}
else {
    // write to console
    // code
}
于 2013-02-20T17:07:26.277 回答