Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to modify System.out.println()

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to modify System.out.println()

    Hi everyone,

    How can I change the System.out.println(x) to allways act as System.out.println(this + x)

    The reason is that I have a project with thousand of java-files and in every one of them there are several System.out.println(). I would like to know what java class is printing on the console. I thought maybe there is a way to modify the System.out.print/println in the main Java library so it would allways do as write the class name or some kind of clue about which class it is first and then the content of println ... But I don't know how?

    Can I modify System.out.println in Java??? Is this possible at all? //Thanks


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: How to modify System.out.println()

    No you cannot alter the Java API source code.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to modify System.out.println()

    Hello emailkia. Welcome to the Java Programming Forums.

    Please do not post duplicate threads. Your other thread has been deleted.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to modify System.out.println()

    Hello,

    you could create your own PrintStream and set that as System.out, try something like this.

    import java.io.OutputStream;
    import java.io.PrintStream;
     
    public class SysOutTest {
     
        public static void main(String[] args) {
            System.out.println("This line does not have the extra text added");
     
            System.setOut(new MyPrintStream(" ADDED TEXT", System.out));
     
            System.out.println("This line will have some added text");
        }
     
        public static class MyPrintStream extends PrintStream {
     
            private final String x;
     
            public MyPrintStream(final String x, final OutputStream outputStream) {
                super(outputStream);
                this.x = x;
            }
     
            @Override
            public void println(final String string) {
                super.println(string + this.x);
            }
        }
    }

    I've left most of the other print methods out in this sample but if you need to implement more of them just copy the contents of the println method.

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    JavaPF (April 12th, 2011)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to modify System.out.println()

    Thank you all

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to modify System.out.println()

    This thread has been cross posted here:

    http://www.java-forums.org/advanced-java/42266-how-modify-system-out-println.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Member
    Join Date
    Mar 2011
    Posts
    32
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to modify System.out.println()

    Yes there is a way to print the classname in java console.
    For that you need to override the toString() method in the class created by you. From the toString() method return the name of the class and use “this” keyword in println() method .
    Following is the codesnnipet:

    package Misc;
    public class B {
    public String toString(){
    return "B";
    }
    public void testPrintln(){
    String str = "test string";
    System.out.println(this+str);
    }
    public static void main(String[] arg){
    new B().testPrintln();
    }
    }

    Hope this is your solution..

Similar Threads

  1. System.out.println ("Hellow");
    By OnionzRingz in forum Member Introductions
    Replies: 1
    Last Post: March 1st, 2011, 05:56 PM
  2. System.out.println("Hi!");
    By 342-173=147 in forum Member Introductions
    Replies: 1
    Last Post: February 27th, 2011, 03:07 PM
  3. How can i print rows 1 by 1 using System.out.println?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2010, 08:35 AM
  4. System.out.println ("Hello world!")
    By ShaunB in forum Member Introductions
    Replies: 0
    Last Post: December 29th, 2009, 05:17 PM