Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

Java realtime writing file when it's opened

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...