Guest User

Untitled

a guest
Nov 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public class Logger extends PrintStream {
  2. static int count;
  3. String directory = "D:/Checknewfiles";
  4. FileWriter output_file;
  5.  
  6. public Logger(OutputStream out) {
  7. super(out);
  8. try{
  9. this.output_file = new FileWriter(new File(directory,"log1.txt"));
  10. }catch (IOException err){
  11. System.out.println("Can't create file");
  12. }
  13. }
  14. public void print(String s){
  15. String forPrinting = count + ": "+ s;
  16. super.print(forPrinting);
  17. try {
  18. output_file.write(forPrinting);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. public void println(String s){
  24. String forPrinting = ++count + ": "+ s;
  25. super.println(forPrinting);
  26. try {
  27. output_file.write(forPrinting);
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33.  
  34. import logger.Logger;
  35.  
  36. public class Main {
  37. public static void main(String[] args) {
  38. System.setOut(new Logger(System.out));
  39. System.out.print("Compute 1/2 ...");
  40. System.out.println("; result: " +(1/2));
  41. try {
  42. int a = 27 / 0;
  43. }
  44. catch (ArithmeticException err){
  45. System.out.println("Something goes wrong");
  46. }
  47. System.out.println("Done computing");
  48. }
  49. }
Add Comment
Please, Sign In to add comment