简体   繁体   English

读写Java中的控制台

[英]Reading and writing to console in Java

在Java中从控制台读取和写入控制台最有效的类是什么?

Java 1.6 has a Console class that simplifies some things. Java 1.6的Console类简化了某些事情。 In general, System.out and Scanner on top of System.in are usually enough. 通常,System.out和位于System.in之上的Scanner通常就足够了。

类似于System 。{out,in}?

BufferedReader and BufferedWriter are the most efficient means of writing to and reading from the console, as well as any file. BufferedReaderBufferedWriter是向控制台以及任何文件进行写入和读取的最有效方法。 However, they are relatively difficult to use. 但是,它们相对难以使用。

Scanner provides an alternative to retrieving input from the command line, and is much easier to use than BufferedReader. 扫描程序提供了一种从命令行检索输入的替代方法,并且比BufferedReader易于使用。

If you are printing log messages in an application, should use a logging API (log4j or whatever). 如果要在应用程序中打印日志消息,则应使用日志记录API(log4j或其他)。 You then specify which levels (debug, info, warn, ...) print on the console per class. 然后,您可以指定每个类在控制台上打印的级别(调试,信息,警告等)。

Logging tends to slow things down dramatically in my experience - so don't log within a tight loop (unless that logging level is typically off in production). 日志记录往往会大大降低我的经验,因此请不要在紧密的循环中进行日志记录(除非日志记录级别通常在生产中处于关闭状态)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM