-1

How able to change System.out which I use to check the result.
I need test this method. Better do this when output will be with PrintStream.
How able to solve this?

Code:

private void scan(File file) {
        Scanner scanner = null;
        int matches = 0;

        try {
            scanner = new Scanner(file);
        } catch (FileNotFoundException e) {
            System.out.println("File Not Found.");
            e.printStackTrace();
        }

        while (scanner.hasNext())
            if (scanner.next().equals(whatFind)) {
                matches++;
            }

        if (matches > 0) {
            String myStr = String.format(
                    "File: %s - and the number of matches " + "is: %d",
                    file.getAbsolutePath(), matches);
            System.out.println(myStr);
        }
    }

Question:

  • How to refactor output System.out to PrintStream?
catch23
  • 17,519
  • 42
  • 144
  • 217

2 Answers2

1

Try using this
PrintWriter out = new PrintWriter(System.out);

In the end do not forget to close it.
out.close();

Note: out println() is faster than System.out.println()

UPDATED

import java.io.PrintStream;
import java.io.PrintWriter;

public class TimeChecker 
{
    public static void main(String[] args) 
    {
        /**
         * Normal System.out.println
         */
        long start = System.currentTimeMillis();
        for(int i=1; i<1000000000; i++);
        long end = System.currentTimeMillis();
        System.out.println((end-start));

        /**
         * Using PrintWriter
         * 
         * Note: The output is displayed only when you write "out.close()"
         * Till then it's in buffer. So once you write close() 
         * then output is printed
         */
        PrintWriter out = new PrintWriter(System.out);
        start = System.currentTimeMillis();
        for(int i=1; i<1000000000; i++);
        end = System.currentTimeMillis();
        out.println((end-start));

        /**
         * Using PrintStream
         */
        PrintStream ps = new PrintStream(System.out, true);
        System.setOut(ps);
        start = System.currentTimeMillis();
        for(int i=1; i<1000000000; i++);
        end = System.currentTimeMillis();
        ps.println((end-start));

        // You need to close this for PrintWriter to display result
        out.close();
    }

}

This will give you the idea how they work and differ from one another.
Hope this helps!!

asifsid88
  • 4,631
  • 20
  • 30
  • @nazar_art I do not understand "Program work endless".. can you explain – asifsid88 Mar 06 '13 at 17:50
  • With `System.out` run time 12s. With this variants not finish run. Waited long time, more than 3 min. After change back all work ok. – catch23 Mar 06 '13 at 17:55
  • Why does this happen? How able to circumvent this? – catch23 Mar 07 '13 at 10:03
  • http://stackoverflow.com/questions/2822005/java-difference-between-printstream-and-printwriter – asifsid88 Mar 07 '13 at 12:56
  • I add some identifi lines. Result is next: `Using System.out.println: 984 Using PrintStream: 1469 Using PrintWriter: 1469`. – catch23 Mar 13 '13 at 09:18
  • Apologized but I did not understand your comment – asifsid88 Mar 13 '13 at 09:51
  • I added next identifi lines to each variants - `System.out.print("Using System.out.println: "); System.out.println((end-start) + " ms;");`... . And Output is next `Using System.out.println: 953 ms; Using PrintStream: 1422 ms; Using PrintWriter: 953 ms;`. Clear? – catch23 Mar 13 '13 at 10:09
  • Yes I got you now. So what are you trying to prove with this? If the answer helped you then accept it otherwise leave it. If you need, post new question with new understanding and error. – asifsid88 Mar 13 '13 at 10:13
0

Try like this: PrintStream anonymous object which will not warrant to close the stream. But PrintWriter warrants.

new PrintStream(System.out).print(str);    

This answer I got from PrintStream programming.

  • We don't need close `PrintStream`? And most important incorrectly work. If `System.out` find 10 files with matches this variant only one + doble run time! – catch23 Mar 06 '13 at 16:34