使用“时 System.out.println 不打印到文件” >文件名”
我有一个特殊的问题。我有一个使用以下命令运行的 Java 程序:
cat input_file_name | ./script_name > file_output_name
script_name
脚本的作用是: java myprogram
我的问题是:如何在控制台中打印出某些内容而不需要它“放入 file_output_name 文件”(因为 > file
将所有 System.out.prints
放入该文件中)
我知道这是可能的,因为已经有一些了从图书馆的某个班级我正在使用我的java程序。但是我无法找到这些打印的确切来源,所以我不知道它是如何编码的。
感谢您的帮助。
I have a peculiar problem. I have a Java program that is run with the command :
cat input_file_name | ./script_name > file_output_name
the script_name
script just does : java myprogram
My question is : how can I print out something in the console without it being "put in the file_output_name file" (since the > file
puts all System.out.prints
in that file)
I know this is possible because there are some already that come from some class in a library that I'm using from my java program. However I can't find the exact source of those prints so I don't know how it is coded.
Thank you for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的答案是将这些消息写入
System.err
而不是System.out
。这将起作用,因为
>
重定向标准输出而不是标准错误。 (您可以重定向标准错误,但 shell 语法不同;例如foo 2> /tmp/somefile
。)更复杂的替代方法是更改您的程序,以便可以使用以下名称调用它输出文件作为参数。然后打开文件,将其包装在
PrintWriter
中并写入该流而不是System.out
。通过一些重构,您应该能够构建您的程序,以便它可以以任何方式使用。The simple answer is to write those messages to
System.err
rather thanSystem.out
.This will work since
>
redirects standard output but not standard error. (You can redirect standard error, but the shell syntax is different; e.g.foo 2> /tmp/somefile
.)A more complicated alternative is to change your program so that it can be called with the name of the output file as an argument. Then open the file, wrap it in a
PrintWriter
and write to that stream rather thanSystem.out
. With a bit of refactoring, you should be able to structure your program so that it can be used either way.最简单的方法是使用 System.err.println() 而不是 System.out。
它将转到不同的“设备”(stderr 而不是 stdout),并且不会被重定向。
The easiest way is to use
System.err.println()
instead ofSystem.out
.It will go to a different "device" (stderr instead of stdout), and it won't be redirected.
根据您所展示的内容,您仅重定向标准输出(stdout)。您可以向标准错误 (stderr) 写入一些内容,而不是让它显示在控制台上。在 Java 中,这可以通过 System.err.println(...) 和相关方法来完成。
With what you've shown, you're only redirecting the standard out (stdout). You can write something to the standard error (stderr) instead to have it show on the console yet. In Java, this can be done by
System.err.println(...)
, and related methods.请注意,如果 Java 程序写入标准输出,它无法控制其输出重定向到何处,这取决于如何从命令行调用程序。
清楚了这一点后,您只需删除
> 即可将程序的标准输出仅重定向到控制台。命令中的 file_output_name
部分。或者,您可以使用
tee
命令将程序的输出重定向到控制台和文件,看看这个文章 其中解释详细说明如何做。Notice that if a Java program writes to standard output, it can't control where does its output get redirected, that depends on how the program is invoked from the command line.
Having that clear, you can redirect the program's standard output only to the console, by simply removing the
> file_output_name
part from the command.Or, you can redirect the output of the program to both the console and a file by using the
tee
command, take a look at this article which explains in detail how to do it.