我应该使用 System.out.println() 还是其他东西?
我已经用 Java 进行了一段时间的随意编程,但我仍然有一些关于基础知识的棘手问题。我听说我应该使用 System.out.println() 来显示某些人的数据,而其他人则给了我不同的想法(例如 PrintStream 或其他东西)。在 Java 中打印到控制台的最佳方法是什么?
I've been casually programming in Java for a while, but I still have a few burning questions on the fundamentals. I've heard that I should use System.out.println() to display data from some people, and others have given me different ideas (like PrintStream or something else). What's the best way to print to console in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您刚刚开始并想要将一些字符串打印到控制台,那么 System.out.println() 就可以了。了解基础知识后,您就可以深入了解 Java I/O 的细节。
If you are just starting and wanting to print some strings to the console then System.out.println() will be fine. Get your fundamentals down and then you can dive into the finer points of Java I/O.
System.out
是一个PrintStream。如果您的主要目标是与控制台交互,请查看
java.io.Console
。如果您的主要目标是进行某种日志记录,请使用 Java Logging API 或 log4j 等日志记录框架。System.out
is a PrintStream.If your main goal is to interact with a console, look at
java.io.Console
. If your main goal is to have some kind of logging, use a logging framework like the Java Logging API or log4j.System.out 适合打印到控制台。然而,我发现努力使用 java.util.logging 对我来说是更好的选择。 (或某些日志系统。)
这取决于您尝试显示的信息。如果用于控制台用户交互,System.out 是完美的。如果要显示调试信息,日志记录可能是最佳选择,因为它可以让您在需要时对事物有更多的控制权。
(初始设置后,日志记录实用程序几乎与使用 System.out 一样透明。)
System.out is good for printing to the console. However, I found that making the effort to use java.util.logging instead was a better choice for me. (Or some logging system.)
It depends on what information you're attempting to display. If it's for console user-interaction, System.out is perfect. If it's to display debug-ish information, logging is probably the way to go as it lets you have far more control over things should you desire it.
(After the initial set-up, the logging utilities are almost as transparent as using System.out.)
有点取决于你在做什么。我几乎总是建议使用 Java 日志记录 API (java.util.logging)。 System.out.println 更像是一个“调试工具”,但即便如此,您也可以使用 JDK 的日志记录 API 对输出进行更多控制...
Kind of depends on what you are doing. Almost always I would suggest using the Java logging API (java.util.logging). System.out.println is more of a "debug tool" but even then you have much more control over the output using the JDK's logging API...
使用 log4j 并将输出直接输出到 stdout。然后可以根据需要轻松切换到日志文件。
Use log4j and direct output to stdout. Its then easy to switch to logfiles as required.
直接使用 System.out 是最简单且通常最有效的方法。如果您有很多要打印的内容,您可以使用 BufferedOutputStream 进行研究。
通过创建要更有效地打印的字符串可以提高一些效率。如果需要进行大量串联,请查看
StringBuilder
。Using
System.out
directly is the simplest and usually most efficient way. If yo have a lot to be printed you might investigate usingBufferedOutputStream
.There are some efficiencies to be gained by creating the Strings to be printed more efficiently. If there is a lot of concatenation being done, look into
StringBuilder
.你可以设置
System.setOut( new PrintStream( "all-my-sysouts-go-in-this-file.txt" );
然后执行 sysoyts...
你可以使用 baretail...
希望这有帮助..raj
。
yu can set
System.setOut( new PrintStream( "all-my-sysouts-go-in-this-file.txt" );
and then do sysoyts...
yu can track them using baretail...
hope this helps..
raj.
我建议使用 Java Logging API
I will suggest to use Java Logging APIs
是的,打印到控制台的最简单有效的方法通常是
System.out.print() 和 System.out.println() 但如果要发送大量输出,System.out.println() 可能效率低下,因为它是行缓冲的并且做了很多相关的工作到 Unicode 处理。由于缓冲区大小较小,System.out.println() 不太适合以批处理模式处理许多重复输出。每行都会立即刷新。在这种情况下,您可能想要使用
BufferedOutputStream 或 BufferedWriter。
需要注意的是,System.out 并不总是指向
应用程序控制台或终端。 System.out 指向
应用标准输出通道;在 C/C++ 中,它称为 stdout。
它可能会到达控制台,但也可能会到达任何地方
用户决定发送它。例如一个文件,那么打印到 System.out 的所有内容都将最终出现在该文件中
我的应用程序输出.txt。
因此,如果您想发送常规输出,请使用 System.out。如果你
想要发送错误消息或用户通知,并且具有很好的
有机会进入控制台,使用 System.err.print() 和
System.err.println()。
没有可移植的方式来获取“真实”控制台的通道
在 1.6 之前的 Java 版本中。 1.6中,有一个对象类
专门代表控制台:java.io.Console。参考
这个唯一的对象可以使用以下方法从 System 类获取
静态方法console()。
但是,如果您的要求是调试整个应用程序,那么正如每个人所说,使用记录器是最好的方法。
Yes, the simplest and efficient way to print to the console is usually
System.out.print() and System.out.println() but if you have a large amount of output to send, System.out.println() may be inefficient, because it is line-buffered and does a lot work related to Unicode handling. Because of its small buffer size, System.out.println() is not well suited to handle many repetitive outputs in a batch mode. Each line is flushed right away. In that case, you may want to use
a BufferedOutputStream or a BufferedWriter.
It is important to note that System.out does not always go to
the application console or terminal. System.out points to the
application standard output channel; in C/C++ it is called stdout.
That may go to the console, but it may also go anywhere that the
user decides to send it. For example a file then everything printed to System.out will end up in the file
my-app-output.txt.
So, if you want to send regular output, use System.out. If you
want to send an error message or user notification, with a great
chance of going to the console, use System.err.print() and
System.err.println().
There is no portable way to get a channel to the 'real' console
in versions of Java before 1.6. In 1.6, there is an object class
specifically to represent the console: java.io.Console. A reference
to this unique object can be obtained from the System class using
the static method console().
But If your requirement is to debug the whole application then as everyone said using loggers is the best approach.