如何将结果发送到文件并同时输出

时间:2017-09-20 23:22:38

标签: java file output fizzbuzz

这个游戏就像 FizzBu​​zz ,但你可以看到不同的单词。

我需要打印“Nardo”的结果输出(System.out.println)并同时调用文件。

我为 FizzBu​​zz 游戏制作了两个类,另一个用于向文件中发送一些文本。这个工作正常,但我不知道如何将它与 FizzBu​​zz 类结合起来。

我刚刚将类OutputToFile调用到FizzBu​​zz中,但我不知道如何继续。

1)头等舱

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)第二类

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

1 个答案:

答案 0 :(得分:0)

下面的课程会将您拨打的电话分成2个OutputStream。

对于您的程序,首先创建一个FileOutputStream,而不是从文件创建PrintStream。然后从FileOutputStream和System.out创建一个新的SplitOutputStream。使用SplitOutputStream,您所做的任何单个写操作都将写入FileOutputStream和System.out。通过将PrintStream包装在SplitOutputStream上,您现在可以在PrintStream上执行println,输出将同时转到文件和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();
    }
}

或者

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

上面的代码将System.out更改为输出到您的文件加上原始的System.out