开发者

Alternate Statements of System.out.println()

Can anybody please help me by providing different ways of printing 开发者_如何学运维other than System.out.println() statement in Java?


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


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


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


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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜