Reading and writing to console in Java
What is the most effici开发者_JAVA技巧ent class to use to read from and write to console in Java?
Java 1.6 has a Console class that simplifies some things. In general, System.out and Scanner on top of System.in are usually enough.
Like System.{out,in}?
BufferedReader and BufferedWriter are the most efficient means of writing to and reading from the console, as well as any file. 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.
If you are printing log messages in an application, should use a logging API (log4j or whatever). 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).
精彩评论