Java realtime writing file when it's opened

2019-09-30 03:19发布

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.

3条回答
来,给爷笑一个
2楼-- · 2019-09-30 03:55

You're doing it wrong - It's impossible. notepad absolutely ignores it's input while it's running (like most GUI-programs). If you want to show a textbox and write text in it, simply create one with Swing/SWT/...

If you just want to write into a file, just create a new PrintWriter and use it to write files: http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html

查看更多
来,给爷笑一个
3楼-- · 2019-09-30 03:58

You shouldn't try to write through Notepad. Check out PrintWriter.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-30 03:59

Interesting:

Lets deal this in simple way.

1. Save a file test.txt somewhere. 
2. Open that file and keep it opened

In Java write to this file (Standard Code)

FileWriter fw = new FileWriter(new FileOutputStream(new File("c:/test.txt")));
fw.write("ABC")

Now go to notepad file again. I normally used Textpad it does refresh automatically (by an alert) because we changed it behind the scene (In your case through Java).

I hope that will clarify a bit.

To be fare trying to excess the genric notepad exe doesn't gurrantee which file you will write in. I am not sure how windows deal with it because you can open 3 different files at one time and which one you will expect to have your data written through java???

查看更多
登录 后发表回答