Search code examples
javafileoutputfizzbuzz

How to sent a result to a file and to output at the same time


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);
        }
    }
}

Solution

  • 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