简体   繁体   English

Java-如何使用PrintStream / OutputStream打印到命令行

[英]Java - How to use PrintStream/OutputStream to print to the Command Line

I know we can use PrintStream to print lines to a given file: 我知道我们可以使用PrintStream将行打印到给定的文件:

    PrintStream output;
    output = new PrintStream("./temp.txt");
    output .println("some output text"); 

However, can we use PrintStream to print lines to the command line ? 但是,我们可以使用PrintStream在命令行上打印吗?

I've looked through the Java docs and it seems PrintStream constructor can take a file path or an OutputStream (is there a OutputStream subclass that would print to command line?) 我查看了Java文档,似乎PrintStream构造函数可以采用文件路径或OutputStream(是否存在可以打印到命令行的OutputStream子类?)

output = new PrintStream(System.out);

or actually, 或实际上

output = System.out;

Similarly, you can do the same with System.err ... So why don't we just simply use System.out and System.err directly? 同样,您可以对System.err执行相同的操作...那么为什么我们不直接使用System.out和System.err? sout + tab is quite fast to type in IntelliJ sout +选项卡输入IntelliJ的速度非常快

System.out or System.error are already PrintStream and we can use them to print the output to command line without creating a new PrintStream object that you are doing. System.outSystem.error已经是PrintStream ,我们可以使用它们将输出打印到命令行,而无需创建您正在执行的新PrintStream对象。

Advantage of using this printStream is that you can use System.setOut() or ``System.setErr()` to set the printSteam of your choice 使用此printStream的优势在于您可以使用System.setOut()或``System.setErr()`来设置您选择的printSteam

PrintStream output = new PrintStream("./temp.txt");
System.setOut(output); 

Above will override the default Printstream of printing to command line and now calling System.out.println() will print everything in given file(temp.txt) 上面的命令将覆盖默认的Printstream打印到命令行,现在调用System.out.println()将打印给定文件(temp.txt)中的所有内容

The PrintStream class provides methods to write data to another stream. PrintStream类提供了将数据写入另一个流的方法。 To print to command line you can use System.out as out is an object of PrintStream class. 要打印到命令行,可以使用System.out因为out是PrintStream类的对象。

import java.io.*;  
class PrintStreamTest{  
  public static void main(String args[])throws Exception{  
    PrintStream pout=new PrintStream(System.out);  
    pout.println(1900);  
    pout.println("Hello Java");  
    pout.println("Welcome to Java");  
    pout.close();  
  }  
} 

This will give output on command line as: 这将在命令行上显示为:

1900 1900

Hello Java 你好Java

Welcome to Java 欢迎使用Java

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

相关问题 Java PrintStream 立即使用 Scanner 和循环输出到 outputStream - Java PrintStream immediately to outputStream with Scanner and loop Java-具有自定义OutputStream的PrintStream上的异常行为 - Java - Strange behaviour on PrintStream with custom OutputStream 我如何使用java中的printStream参数打印堆栈? - How i print a stack using printStream argument in java? 如何使用打印流 - How to use printStream 如何在Java中的命令行中打印十字形? - How to print a cross shape in command line in Java? 在哪里放置PrintStream对象以打印变量java - Where to put a PrintStream object to print a variable java 为什么我不能在java.io.PrintStream中使用print()或println()方法,因为它是在导入类之后? - Why can't I use the print() or println() method in java.io.PrintStream as it is after importing the class? Java,用什么代替 PrintStream 来获取异常? - Java, what to use instead of PrintStream to get exceptions? 如何使用 PrintStream 写入文本文件而不在每行之间留空行? - How to use PrintStream to write to a text file without leaving any blank line between each row? 如何将字符串中的新行字符插入PrintStream,然后使用扫描程序重新读取该文件 - how to insert a new line character in a string to PrintStream then use a scanner to re-read the file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM