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.
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.htmlYou shouldn't try to write through Notepad. Check out PrintWriter.
Interesting:
Lets deal this in simple way.
In Java write to this file (Standard Code)
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???