Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Logger extends PrintStream {
- static int count;
- String directory = "D:/Checknewfiles";
- FileWriter output_file;
- public Logger(OutputStream out) {
- super(out);
- try{
- this.output_file = new FileWriter(new File(directory,"log1.txt"));
- }catch (IOException err){
- System.out.println("Can't create file");
- }
- }
- public void print(String s){
- String forPrinting = count + ": "+ s;
- super.print(forPrinting);
- try {
- output_file.write(forPrinting);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void println(String s){
- String forPrinting = ++count + ": "+ s;
- super.println(forPrinting);
- try {
- output_file.write(forPrinting);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- import logger.Logger;
- public class Main {
- public static void main(String[] args) {
- System.setOut(new Logger(System.out));
- System.out.print("Compute 1/2 ...");
- System.out.println("; result: " +(1/2));
- try {
- int a = 27 / 0;
- }
- catch (ArithmeticException err){
- System.out.println("Something goes wrong");
- }
- System.out.println("Done computing");
- }
- }
Add Comment
Please, Sign In to add comment