이 게임은 FizzBuzz과 같지만 다른 단어로 볼 수 있습니다.파일에 결과를 보내고 동시에 출력하는 방법
"Nardo"의 결과를 출력하여 (System.out.println
) 동시에 호출 할 때 파일에 기록해야합니다.
두 클래스를 하나 만들었습니다. FizzBuzz 게임과 다른 하나는 파일에 텍스트를 보냈습니다. 이 하나가 올바르게 작동하지만 나는 그것을 FizzBuzz 클래스와 결합하는 방법을 모른다.
방금 OutputToFile 클래스를 FizzBuzz에 호출했지만 계속 진행하는 방법을 모르겠습니다.
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);
}
}
}
콘솔과에하는 기록의 PrintStream를 확장하는 클래스를 작성 출력에 System.out을 변경 파일. 클래스의 인스턴스와 함께 System.setOut() 메서드를 사용하십시오. – NormR
'System.out' (와'System.err')을 다른'PrintStream's로 비교적 쉽게 리다이렉트 할 수 있습니다. 말하자면, 말하기를 두 개의 다른 위치로 보내고 싶을 때입니다 - [이 예제] (https://stackoverflow.com/questions/12945537/how-to-set-output-stream-to-textarea/12945678#12945678) 기본적으로 GUI로 출력물을 출력하기위한 것이지만 기본 아이디어는 같습니다. . 나는 또한 단지 어떤 로깅 API에 대해서도 살펴볼 것을 고려할 것이고, 이는 – MadProgrammer