Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package com.javarush.test.level19.lesson08.task02;
  2.  
  3. /* Ридер обертка 2
  4. В методе main подмените объект System.out написанной вами ридер-оберткой по аналогии с лекцией
  5. Ваша ридер-обертка должна заменять все подстроки "te" на "??"
  6. Вызовите готовый метод printSomething(), воспользуйтесь testString
  7. Верните переменной System.out первоначальный поток
  8. Вывести модифицированную строку в консоль.
  9. */
  10.  
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.PrintStream;
  13.  
  14. public class Solution {
  15. public static TestString testString = new TestString();
  16.  
  17. public static void main(String[] args) {
  18. PrintStream consoleStream = System.out; //save default output to console
  19.  
  20. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //new adaptive byte array
  21. PrintStream stream = new PrintStream(outputStream); //new adapter fot PrintStream class
  22. System.setOut(stream);//redirect output to array
  23. testString.printSomething();//output to array
  24. String result = outputStream.toString().replaceAll("te","??");//adopting output string
  25. System.setOut(consoleStream); // return out put to console
  26. System.out.println(result); //output new string
  27.  
  28.  
  29. }
  30.  
  31. public static class TestString {
  32. public void printSomething() {
  33. System.out.println("it's a text for testing");
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement