简体   繁体   中英

Using user defined method to print line to console in java

I have simple program like this

class println 
{
  public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }
}

But i dont want to use System.out.println to print..i want to use my own user defined method to print the data.

Bear in mind that Java code runs in the JVM, and the JVM only communicates to the outside world in a few specified ways. So if you want to print to a console, you will need to call a method that somehow (after JNI, etc.) gets mapped to an IO operation on your actual OS. Unless you plan on building a new OS-level IO operation (in which case you can't write it in Java anyway), your best bet is to use an existing class that already knows how to talk to the outside world.

As it happens, the java.io.PrintStream class knows how to write to an OS level file stream, and the OS provides a file stream for the console ( stdout ). (Technically, PrintStream only knows how to write to an OutputStream , and OutputStream knows how to write to an OS level file stream, but I don't want to get bogged down in details.) Java provides an instance of PrintStream already attached to your process's standard output stream; that PrintStream object can be referenced as the out property of the java.lang.System object. Given that PrintStream has methods like println() , you can write:

java.lang.System.out.println("Hello, world!");

Since you always get java.lang.* imported by default, you can shorten that to:

System.out.println("Hello, world!");

Alternatively, you can assign the PrintStream instance to a local variable:

import java.io.PrintStream;

public class HelloWorld {
    public static void main(String[] args) {
        PrintStream p = System.out;
        p.println("Hello, world!");
    }
}

What does your "user defined method to print" do that the println() method of PrintStream objects doesn't? If you're interested in formatting the output, then you need to format it as a String before you call the println() method. As a convenience, you can wrap up the formatting and actual printing in a single helper method. Is this what you're actually looking for?

A wild guess here: is this for logging purposes? I'd guess that a good proportion of people who want to write to the console are doing so for logging purposes and that a very high proportion of those people don't want to use System.out.println() so that they can have greater control of switching that particular form of logging on and off.

If my guess is correct, might I suggest looking into a logging framework like Log4J or SLF4J / Logbac k? You'll get console appenders and whatever degree of control you need.

System is a final class from the java.lang package. out is a class variable of type PrintStream declared in the System class. println is a method of the PrintStream class.

So, if you want to overwrite prinln method, define your own method instead and pass the Printstrem object in that method.

Method 2:
Create your own PrintStream - eg public class YourPrintStream extends PrinterStream . Override the print(String s) method and change the string there any way you like. Then call super.print(s));
Call System.setOut(new YourPrintStream())
Then everytime System.out.println is called, the passed string will be under your control before going into the actual stream.

class HelloWorld 
{
  public static void main(String[] args) 
  {
    myPrint("Hello World!");
  }

  public static void myPrint(String message) {
    System.out.print(message + "\n");
  }
}

System.out.println is a call of the method println of the static instance out (class PrintStream ) of the class System .

In Java, there is no way to rename existing methods or instance fields (ie you can't change the name of println or out to anything else) and System.out is the only way to get at the value of out (which connects you to the console).

So there is no way to achieve what you want.

Let's say you want to intercept all content going to System.out.println() . You can do the following:

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(buffer);
System.setOut(printStream);

System.out.println("Hello!");

String whatWasPrinted = new String(buffer.getBytes());

Using techniques like this, you can supply a custom stream with custom "printing" code to do what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM