Automation Using Selenium Webdriver

Thursday 22 December 2016

Java example program to print message without using System.out.println()

Is it possible to print message without using system.out.println?




Yes its possible to print message without using System.out.println("");

System.out.write("www.corejavawithselenium.blogspot.in\n".getBytes());
 System.out.format("%s", "www.java.com \n")
 PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
   myout.print("www.corejavawithselenium.blogspot.in \n");
System.err.print("This is custom error message");
System.console().writer().println("Hai");

1.System.out.write(Byte [] arg0);

System.out.write(Byte [] arg0) method The java.io.FilterOutputStream.write(byte[] b) method writes b.length bytes to this output stream.

package com.instanceofjava;

public class PrintMessage {

public static void main(String[] args) throws IOException{

       System.out.write("Hello World".getBytes());
}
}

Output:

Hello World

2. PrintStream  java.io.PrintStream.format(String arg0, Object... arg1)

System.out.format("format",Object obj) by using this method we can format the text and print

package com.instanceofjava;

public class PrintMessage {

public static void main(String[] args){

        System.out.format("%s", "James");
}

}

Output:

James

3. FileDescriptor:

Create PrintStream object by passing FileOutputStream(FileDescriptor.out) object as an argument to the constructor
package com.instanceofjava;

public class PrintMessage {

public static void main(String[] args){

       PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
          myout.print("i love Java");
}
}

Output:

i love java


4. System.err.print(""):

We can use System.err.print("") method to print the message actually its used to print error message
package com.instanceofjava;

public static void main(String[] args){

       System.err.print("This is custom error message");
}
}

Output:

This is custom error message


5.System.console().writer().println("");

System.console() returns null if your application is not run in a terminal System.console() provides methods for reading password without echoing characters

package com.instanceofjava;

public static void main(String[] args){

       System.err.print("This is custom error message");
}
}

Output:

This is custom error message Exception in thread "main" java.lang.NullPointerException
    at PrintMessage.main(PrintMessage.java:24)

No comments:

Post a Comment