java System.out.println() 的替代语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5945593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:36:09  来源:igfitidea点击:

Alternate Statements of System.out.println()

javaprinting

提问by hari

Can anybody please help me by providing different ways of printing other than System.out.println() statement in Java?

任何人都可以通过在 Java 中提供除 System.out.println() 语句之外的不同打印方式来帮助我吗?

回答by Georgian Citizen

import org.apache.log4j.Logger;
....
public class example{

static Logger log = Logger.getLogger(this.class);

.....
public void test(){
 String hello ="Hello World";
 log.trace(hello);
}
....
}

output will be :
TRACE:(<classname>){2011-10-38-06:644} Hello World 2011-05-10 08:38:06,644

回答by ponds

This may help you.

这可能对你有帮助。

import java.io.*;
class Redirection {
    public static void main(String args[]) throws IOException {
        PrintStream pos = new PrintStream(new FileOutputStream("applic.log"));

        PrintStream oldstream=System.out;
        System.out.println("Message 1 appears on console");
        System.setOut(pos);                 
        System.out.println("Message 2 appears on file"); 
        System.out.println("Message 3 appears on file");
        System.out.println("Message 4 appears on file");
        System.setOut(oldstream);
        System.out.println("Message 5 appears on console");
        System.out.println("Message 6 appears on console");        
    }
}

回答by Raze

Alternate printing methods:

替代印刷方法:

System.out.print("message\r\n");
System.out.printf("%s %d", "message" , 101); // Since 1.5

You can also use regular IO File operations by using special files based on the platform to output stuff on the console:

您还可以通过使用基于平台的特殊文件在控制台上输出内容来使用常规 IO 文件操作:

PrintWriter pw = new PrintWriter("con"); // Windows
PrintWriter pw = new PrintWriter("/dev/tty"); // *nix

pw.println("op");

回答by sudmong

System.err.println() for printing on console. or create your own printstream object and then print to file, database or console.

System.err.println() 用于在控制台上打印。或创建您自己的打印流对象,然后打印到文件、数据库或控制台。

回答by james

you can solve it in eclipse by placing a mouse on the word a pop-up window will appear scroll down and select JAVA.LANG.SYSTEM.It will fix the problem and your code will run.

您可以在 eclipse 中通过将鼠标放在单词上来解决它,一个弹出窗口将出现向下滚动并选择。JAVA.LANG.SYSTEM它将解决问题并且您的代码将运行。

回答by Mohit Arora

You can try the following alternatives:

您可以尝试以下替代方案:

1.

1.

System.err.print("Custom Error Message");

2.

2.

System.console().writer().println("Hello World");

3.

3.

System.out.write("www.stackoverflow.com \n".getBytes());

4.

4.

System.out.format("%s", "www.stackoverflow.com \n");

5.

5.

PrintStream myout = new PrintStream(new FileOutputStream(FileDescriptor.out));
myout.print("www.stackoverflow.com \n");

回答by Ramindu Samarawickrama

You can also try the code System.out.printf();

你也可以试试代码 System.out.printf();