System.out.println() 的备用语句

发布于 2024-11-06 10:08:05 字数 67 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

微暖i 2024-11-13 10:08:05
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
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
篱下浅笙歌 2024-11-13 10:08:05

替代打印方法:

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

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

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

pw.println("op");

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:

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

pw.println("op");
微暖i 2024-11-13 10:08:05

这可能对你有帮助。

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");        
    }
}

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");        
    }
}
不必你懂 2024-11-13 10:08:05

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

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

心清如水 2024-11-13 10:08:05

以下替代

1.2.3.4.5

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

您可以尝试

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

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

方案

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

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

You can try the following alternatives:

1.

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

2.

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

3.

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

4.

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

5.

PrintStream myout = new PrintStream(new FileOutputStream(FileDescriptor.out));
myout.print("www.stackoverflow.com \n");
捂风挽笑 2024-11-13 10:08:05

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

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.

土豪 2024-11-13 10:08:05

您还可以尝试代码 System.out.printf();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文