-2

How to save an unsaved notepad file using java ? that notepad file is not created by my program. and the condition is there is only one notepad file opened. i have used Process and Runtime class but not able to understand how to save the file because i dont know the path of file.

nikhil
  • 5
  • 7

1 Answers1

0

Some help, but it would be more robust to do it in C/C++/C# !

1 you must find your Notepad app.

it is note easy, and sure. See this: Find out what application (window) is in focus in Java

you can use JNI or JNA.

2 if you know where your app is , send a ctrl^s : see this for sending keystroke to an app. It can work if nothing is currently done inside:

Robot robot = new Robot();
robot.mouseMove(100, 100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
 robot.keyPress(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_S);
 robot.keyRelease(KeyEvent.VK_S);
 robot.keyRelease(KeyEvent.VK_CONTROL);

How to programmatically send a key event to any window/process in a Java App?

Note that:

  • it works if notepad is in front of your screen. Otherwise
  • it works if the file has already a name. If not, it only open a dialog to choose the file. You can also send a file name.
Community
  • 1
  • 1