Skip to content

Instantly share code, notes, and snippets.

@fermopili
Created March 27, 2017 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fermopili/3f0c51b9370a9e4b8c82d2e2037a17e7 to your computer and use it in GitHub Desktop.
Save fermopili/3f0c51b9370a9e4b8c82d2e2037a17e7 to your computer and use it in GitHub Desktop.
com.javarush.task.task19.task1915
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