Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. package com.javarush.task.task19.task1915;
  2.  
  3. /*
  4. Дублируем текст
  5. */
  6.  
  7. import java.io.*;
  8.  
  9. public class Solution
  10. {
  11. public static TestString testString = new TestString ( );
  12.  
  13. public static void main(String[] args) throws IOException
  14. {
  15. BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
  16. String nameF = reader.readLine ( );
  17. // nameF = "c://1";
  18. //запоминаем настоящий PrintStream в специальную переменную
  19. PrintStream consoleStream = System.out;
  20. //Создаем динамический массив
  21. ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( );
  22. //создаем адаптер к классу PrintStream
  23. PrintStream stream = new PrintStream (outputStream);
  24. //Устанавливаем его как текущий System.out
  25. System.setOut (stream);
  26. testString.printSomething ( );
  27. FileOutputStream fileWriter = new FileOutputStream (nameF);
  28. fileWriter.write (outputStream.toByteArray ( ));
  29. //Возвращаем все как было
  30. System.setOut (consoleStream);
  31. // дублируем в консоль
  32. System.out.print (outputStream.toString ( ));
  33. reader.close ( );
  34. fileWriter.close ( );
  35. }
  36.  
  37. public static class TestString
  38. {
  39. public void printSomething()
  40. {
  41. System.out.println ("it's a text for testing");
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement