Created
March 27, 2017 04:53
-
-
Save fermopili/3f0c51b9370a9e4b8c82d2e2037a17e7 to your computer and use it in GitHub Desktop.
com.javarush.task.task19.task1915
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task19.task1915; | |
/* | |
Дублируем текст | |
*/ | |
import java.io.*; | |
public class Solution | |
{ | |
public static TestString testString = new TestString ( ); | |
public static void main(String[] args) throws IOException | |
{ | |
BufferedReader reader = new BufferedReader (new InputStreamReader (System.in)); | |
String nameF = reader.readLine ( ); | |
// nameF = "c://1"; | |
//запоминаем настоящий PrintStream в специальную переменную | |
PrintStream consoleStream = System.out; | |
//Создаем динамический массив | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ); | |
//создаем адаптер к классу PrintStream | |
PrintStream stream = new PrintStream (outputStream); | |
//Устанавливаем его как текущий System.out | |
System.setOut (stream); | |
testString.printSomething ( ); | |
FileOutputStream fileWriter = new FileOutputStream (nameF); | |
fileWriter.write (outputStream.toByteArray ( )); | |
//Возвращаем все как было | |
System.setOut (consoleStream); | |
// дублируем в консоль | |
System.out.print (outputStream.toString ( )); | |
reader.close ( ); | |
fileWriter.close ( ); | |
} | |
public static class TestString | |
{ | |
public void printSomething() | |
{ | |
System.out.println ("it's a text for testing"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment