The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java example program to print message without using System.out.println() Posted: Mar 10, 2016 8:43 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java example program to print message without using System.out.println()
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
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)

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

Topic: Kids : HouseHold Items v9 Previous Topic   Next Topic Topic: How to create shippable desktop applications in java using Eclipse IDE

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use