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


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

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

print without system.out.println()



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.

  1. package com.instanceofjava;
  2.  
  3. public class PrintMessage {

  4. public static void main(String[] args) throws IOException{
  5.  
  6.        System.out.write("Hello World".getBytes());
  7. }
  8. }

Output:

  1. 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

  1. package com.instanceofjava;
  2.  
  3. public class PrintMessage {

  4. public static void main(String[] args){
  5.  
  6.         System.out.format("%s", "James");
  7. }
  8.  
  9. }

Output:

  1. James

3. FileDescriptor:

  • Create PrintStream object by passing FileOutputStream(FileDescriptor.out) object as an argument to the constructor
  1. package com.instanceofjava;
  2.  
  3. public class PrintMessage {

  4. public static void main(String[] args){
  5.  
  6.        PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
              myout.print("i love Java");
  7. }
  8. }

Output:

  1. 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
  1. package com.instanceofjava;
  2.  
  3. public static void main(String[] args){
  4.  
  5.        System.err.print("This is custom error message");
  6. }
  7. }

Output:

  1. 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

  1. package com.instanceofjava;
  2.  
  3. public static void main(String[] args){
  4.  
  5.        System.err.print("This is custom error message");
  6. }
  7. }

Output:

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

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu