Home » Java » Java realtime writing file when it's opened

Java realtime writing file when it's opened

Posted by: admin May 17, 2018 Leave a comment

Questions:

I want to write data to file when it’s opened, but it doesn’t work. Calendar getTime works nice, System.out.println() proves this. Please, any idea, what’s wrong…?

Main class:

 public static void main(String[] args) throws IOException {
        // TODO code application logic here
        CurrentTime ct = new CurrentTime();
    }

CurrentTime class:

public class CurrentTime {

    public OutputStream output;
    public InputStream input;
    public Process npp;

    CurrentTime() throws IOException
    {
        Timer t = new Timer();
        npp =  Runtime.getRuntime().exec("notepad");
        output = npp.getOutputStream();        

        TimerTask task = new TimerTask() {
            @Override
            public void run()
            {                 
                String dateStr = Calendar.getInstance(new Locale("ua", "UA")).getTime().toString();
                System.out.println(dateStr);

                try {
                    output.write(dateStr.getBytes());
                    output.flush();                          
                } catch (IOException ex) {                    
                    Logger.getLogger(CurrentTime.class.getName()).log(Level.SEVERE, null, ex);
                }                
            }
        };
        t.schedule(task, 1000, 2000);

    }
}

Maybe this code is wrong in all, np. In this way, I want to discover this moment by any side, is it impossible at all?

UPDATE: it’s not actual anymore but just for a note, that time I was trying to implement some kind of tailing operation to the text editor directly and now I understand how abnormal this idea was.. had to be implemented using totally other way of course.

Answers: