0

This game is like FizzBuzz but with different words as you can see.

I need to print the result of "Nardo" to output(System.out.println) and to file, when called, at the same time.

I made 2 classes one for FizzBuzz game and the other to sent some text into the file. This one works correctly, but I don't know how to combine it with FizzBuzz Class.

I just called the class OutputToFile into the FizzBuzz, but I don't know how to continue.

1) First Class

public class BaroSello {    
    private OutputToFile outputfile = new OutputToFile();
    for(int i=1; i<input; i++){
        if ((i % 3 == 0 && i % 5 == 0))
            System.out.println("BaroSello");
        else if (i % 3 == 0)
            System.out.println("Baro");
        else if (i % 5 == 0)
            System.out.println("Sello");
        else if (i%7==0)
        // here I need the output to file and to terminal
            System.out.println("Nardo");
        else
            System.out.println(i);
    }
}

2) Second Class

    public class OutputToFile {

    public static void main(String[] args) {
        try {
            PrintStream myconsole = new PrintStream(new File("/Users/xxx/Desktop/output.txt"));
            System.setOut(myconsole);
            myconsole.println("hello");
        } catch (FileNotFoundException fx) {

            System.out.println(fx);
        }
    }
}
rm - rf
  • 3
  • 3
  • Write a class that extends PrintStream that writes to console and to a file. Use the System.setOut() method with an instance of your class. – NormR Sep 20 '17 at 23:39
  • You can redirect `System.out` (and `System.err`) to different `PrintStream`s relatively easily, the trick is when, as you say, you want to send it to two different locations - [this example](https://stackoverflow.com/questions/12945537/how-to-set-output-stream-to-textarea/12945678#12945678) does basically that, while it's intended to print output to a GUI, the basic idea is the same. I'd also consider having a look at just about any logging API, which would do this as well – MadProgrammer Sep 20 '17 at 23:50

1 Answers1

0

The class below will split the calls you make to a 2 OutputStream's.

For your program, instead of creating a PrintStream from the file, first create a FileOutputStream. Then create a new SplitOutputStream from your FileOutputStream and System.out. With the SplitOutputStream, any single write you make will be written to both the FileOutputStream and System.out. By wrapping a PrintStream over the SplitOutputStream, you can now do println on the PrintStream, and the output will go to both the file and the System.out.

import java.io.*;

public class SplitOutputStream extends OutputStream{

    public static void main(String[] args) throws Throwable{
        SplitOutputStream split=new SplitOutputStream(
            new FileOutputStream("c:\\text.txt"),
            System.out
        );
        PrintStream splitPs=new PrintStream(split);
        splitPs.println("some text");
        splitPs.flush();
        splitPs.close();

        //or (not both)
        PrintWriter splitPw=new PrintWriter(split);
        splitPw.println("some text");
        splitPw.flush();
        splitPw.close();
    }


    OutputStream o1;
    OutputStream o2;
    public SplitOutputStream(OutputStream o1,OutputStream o2){
        this.o1=o1;
        this.o2=o2;
    }
    @Override public void write(int b) throws IOException{
        o1.write(b);
        o2.write(b);
    }
    @Override public void write(byte[] b,int off,int len) throws IOException{
        o1.write(b,off,len);
        o2.write(b,off,len);
    }
    @Override public void flush() throws IOException{
        o1.flush();
        o2.flush();
    }
    @Override public void close() throws IOException{
        o1.close();
        o2.close();
    }
}

Or

    PrintStream originalSysOut=System.out;

    SplitOutputStream split=new SplitOutputStream(
        new FileOutputStream("c:\\work\\text.txt"),
        originalSysOut
    );
    PrintStream splitPs=new PrintStream(split,true);

    System.setOut(splitPs);

    System.out.println("some text");

The code above changes the System.out to output to your file plus the original System.out

Fai Ng
  • 760
  • 1
  • 6
  • 14
  • how I can call this class into FizzBuzz algorithm? – rm - rf Sep 21 '17 at 06:30
  • if I have understand, I can use my class of _FizzBuzz_ without modifying it and my `System.out.println` will go directly to file and terminal? How can select one of them, as requested? But If I need to change _FizzBuzz_ to connect onto your new Class, how can do that? – rm - rf Sep 21 '17 at 16:51