如何将结果发送到文件并同时输出 [英] How to sent a result to a file and to output at the same time

查看:47
本文介绍了如何将结果发送到文件并同时输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该游戏类似于 FizzBu​​zz ,但是您看到的单词不同.

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

我需要将"Nardo"的结果打印到输出( System.out.println )上,并在调用时同时打印到文件上.

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

我为FemBuzz 游戏设置了2个类,另一个类将一些文本发送到文件中.这个可以正常工作,但是我不知道如何将其与 FizzBu​​zz 类结合使用.

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.

我刚刚在FizzBu​​zz中调用了OutputToFile类,但是我不知道如何继续.

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

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

推荐答案

下面的类会将您进行的调用划分为2个OutputStream.

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

对于您的程序,首先从文件中创建FileOutputStream,而不是从文件中创建PrintStream.然后从您的FileOutputStream和System.out创建一个新的SplitOutputStream.使用SplitOutputStream,您所做的任何单次写入都将同时写入FileOutputStream和System.out.通过将PrintStream包裹在SplitOutputStream上,您现在可以在PrintStream上执行println,并且输出将同时到达文件和System.out.

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

    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的输出

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

这篇关于如何将结果发送到文件并同时输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆