console.writeline and System.out.println

后端 未结 4 677
一生所求
一生所求 2020-11-28 21:50

What exactly is the technical difference between console.writeline and System.out.println? I know that System.out.println writes to s

相关标签:
4条回答
  • 2020-11-28 21:57

    There's no Console.writeline in Java. Its in .NET.

    Console and standard out are not same. If you read the Javadoc page you mentioned, you will see that an application can have access to a console only if it is invoked from the command line and the output is not redirected like this

    java -jar MyApp.jar > MyApp.log
    

    Other such cases are covered in SimonJ's answer, though he missed out on the point that there's no Console.writeline.

    0 讨论(0)
  • 2020-11-28 22:05

    They're essentially the same, if your program is run from an interactive prompt and you haven't redirected stdin or stdout:

    public class ConsoleTest {
        public static void main(String[] args) {
            System.out.println("Console is: " + System.console());
        }
    }
    

    results in:

    $ java ConsoleTest
    Console is: java.io.Console@2747ee05
    $ java ConsoleTest </dev/null
    Console is: null
    $ java ConsoleTest | cat
    Console is: null
    

    The reason Console exists is to provide features that are useful in the specific case that you're being run from an interactive command line:

    • secure password entry (hard to do cross-platform)
    • synchronisation (multiple threads can prompt for input and Console will queue them up nicely, whereas if you used System.in/out then all of the prompts would appear simultaneously).

    Notice above that redirecting even one of the streams results in System.console() returning null; another irritation is that there's often no Console object available when spawned from another program such as Eclipse or Maven.

    0 讨论(0)
  • 2020-11-28 22:12

    First I am afraid your question contains a little mistake. There is not method writeline in class Console. Instead class Console provides method writer() that returns PrintWriter. This print writer has println().

    Now what is the difference between

    System.console().writer().println("hello from console");
    

    and

    System.out.println("hello system out");
    

    If you run your application from command line I think there is no difference. But if console is unavailable System.console() returns null while System.out still exists. This may happen if you invoke your application and perform redirect of STDOUT to file.

    Here is an example I have just implemented.

    import java.io.Console;
    
    
    public class TestConsole {
        public static void main(String[] args) {
            Console console = System.console();
            System.out.println("console=" + console);
            console.writer().println("hello from console");
        }
    }
    

    When I ran the application from command prompt I got the following:

    $ java TestConsole
    console=java.io.Console@93dcd
    hello from console
    

    but when I redirected the STDOUT to file...

    $ java TestConsole >/tmp/test
    Exception in thread "main" java.lang.NullPointerException
            at TestConsole.main(TestConsole.java:8)
    

    Line 8 is console.writer().println().

    Here is the content of /tmp/test

    console=null
    

    I hope my explanations help.

    0 讨论(0)
  • 2020-11-28 22:17

    Here are the primary differences between using System.out/.err/.in and System.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 = { "\u250C\u2500\u2500\u2500\u2500\u2500\u2510", 
            "\u2502Hello\u2502",
            "\u2514\u2500\u2500\u2500\u2500\u2500\u2518" };
        for (String s : data) {
          System.out.println(s);
        }
        for (String s : data) {
          System.console().writer().println(s);
        }
      }
    }
    

    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│
    └─────┘
    

    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.

    0 讨论(0)
提交回复
热议问题