console.writeline 和 System.out.println [英] console.writeline and System.out.println

查看:20
本文介绍了console.writeline 和 System.out.println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

console.writelineSystem.out.println 之间的技术区别究竟是什么?我知道 System.out.println 写入标准输出,但这与控制台不是一回事吗?

What exactly is the technical difference between console.writeline and System.out.println? I know that System.out.println writes to standard output but is this not the same thing as the console?

我不完全理解文档 用于 console.writeline.

I do not fully understand the documentation for console.writeline.

推荐答案

以下是使用 System.out/.err/.inhref="http://download.oracle.com/javase/6/docs/api/java/lang/System.html#console()" rel="noreferrer">System.console():

Here are the primary differences between using System.out/.err/.in and System.console():

  • System.console() 如果您的应用程序未在终端中运行,则返回 null (虽然你可以在你的应用程序中处理这个)
  • System.console() 提供不回显字符读取密码的方法
  • System.outSystem.err 使用默认的平台编码,而 Console 类输出方法使用控制台编码
  • System.console() returns null if your application is not run in a terminal (though you can handle this in your application)
  • System.console() provides methods for reading password without echoing characters
  • System.out and System.err use the default platform encoding, while the Console class output methods use the console encoding

后一种行为可能不是很明显,但像这样的代码可以证明差异:

This latter behaviour may not be immediately obvious, but code like this can demonstrate the difference:

public class ConsoleDemo {
  public static void main(String[] args) {
    String[] data = { "u250Cu2500u2500u2500u2500u2500u2510", 
        "u2502Hellou2502",
        "u2514u2500u2500u2500u2500u2500u2518" };
    for (String s : data) {
      System.out.println(s);
    }
    for (String s : data) {
      System.console().writer().println(s);
    }
  }
}

在我的系统编码为 windows-1252 和默认控制台编码为 IBM850 的 Windows XP 上,此代码将写入:

On my Windows XP which has a system encoding of windows-1252 and a default console encoding of IBM850, this code will write:

???????
?Hello?
???????
┌─────┐
│Hello│
└─────┘

请注意,此行为取决于将控制台编码设置为与系统编码不同的编码.由于一系列历史原因,这是 Windows 上的默认行为.

Note that this behaviour depends on the console encoding being set to a different encoding to the system encoding. This is the default behaviour on Windows for a bunch of historical reasons.

这篇关于console.writeline 和 System.out.println的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆