Advertisement
Guest User

Untitled

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