Java realtime writing file when it's opened

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:36:40

问题


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.


回答1:


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???




回答2:


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:


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



来源:https://stackoverflow.com/questions/13971495/java-realtime-writing-file-when-its-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!