Alternative Messages System.out.println ()

Can someone help me by providing various printing methods besides the System.out.println () statement in Java?

+3
source share
7 answers
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
+4
source

This can 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");        
    }
}
+4
source

:

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

- :

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

pw.println("op");
+3

System.err.println() . printstream, , .

+2

eclipse, , , JAVA.LANG.SYSTEM. , .

0

:

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

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

0
source

All Articles