Java realtime writing file when it's opened
Asked Answered
L

3

-3

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.

Lupien answered 20/12, 2012 at 11:44 Comment(4)
"Doesn't work" is BAD. How do you see that it doesn't work? Can you specify it?Polysynthetic
Why do you think that writing to the stdin of notepad would write to a file? Most Windows applications don't even care about their stdin/stdout.Trilly
Yes, I can specify it. After launching this program, notepad is opening but then it doesn't output any data, but console is.Lupien
Actually "I can't make it work" would be a better term.Rondelle
L
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???

Laureen answered 20/12, 2012 at 12:2 Comment(0)
P
1

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

Polysynthetic answered 20/12, 2012 at 11:54 Comment(1)
@user1918648: Yes, It's impossible.Polysynthetic
L
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???

Laureen answered 20/12, 2012 at 12:2 Comment(0)
A
0

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

Apomixis answered 20/12, 2012 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.