Is it possible to print message without using system.out.println?
1.System.out.write(Byte [] arg0);
Output:
2. PrintStream java.io.PrintStream.format(String arg0, Object... arg1)
Output:
3. FileDescriptor:
Output:
4. System.err.print(""):
Output:
5.System.console().writer().println("");
Output:
- Yes its possible to print message without using System.out.println("");
- System.out.write("www.instanceofjava.com \n".getBytes());
- System.out.format("%s", "www.instanceofjava.com \n")
- PrintStream myout = new PrintStream(new FileOutputStream(FileDescriptor.out));
myout.print("www.instanceofjava.com \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 messageException in thread "main" java.lang.NullPointerException
at PrintMessage.main(PrintMessage.java:24)
Bagikan
Java example program to print message without using System.out.println()
4/
5
Oleh
Kris Kimcil