Is it possible 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("");

Number of Ways:

1.      System.out.write("www.instanceofjava.com \n".getBytes());
2.      System.out.format("%s", "www.instanceofjava.com \n")
3.      PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
   myout.print("www.instanceofjava.com \n");
4.      System.err.print("This is custom error message");
5.      System.console().writer().println("Hai");

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.

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

FileDescriptor:

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

System.err.print(""):


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


package javaprgms;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class PrintMessage {

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

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

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

                        @SuppressWarnings("resource")
                        PrintStream myoutnew PrintStream(new FileOutputStream(FileDescriptor.out));
                        myout.print("i love Java");

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

Output:

Hello World James i love Java This is custom error message

No comments:

Post a Comment