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.
Asked
Active
Viewed 326 times
-2
-
so ... you want to open a file that doesn't exist yet? – Stultuske Dec 09 '15 at 10:52
-
1*because i dont know the path of file* Because there is no path yet? – Manu Dec 09 '15 at 10:53
-
file is not opened by program – nikhil Dec 09 '15 at 10:54
-
some xyz application created notepad file and i want to save this file – nikhil Dec 09 '15 at 10:56
-
it is possible to save this file ???? – nikhil Dec 09 '15 at 10:57
-
how can i save file plz let me know – nikhil Dec 09 '15 at 11:02
-
I don't understand this question. Please edit your question and add your code – Si mo Dec 09 '15 at 11:09
-
@nikhil: you can not open that file in your Java application, because the file doesn't exist yet. – Stultuske Dec 09 '15 at 11:15
-
but the actual thing is i am not opened this file – nikhil Dec 09 '15 at 11:23
-
my task is to save this file – nikhil Dec 09 '15 at 11:23
-
can i get the instance of that notepad file from process class or can i get the content of notepad file – nikhil Dec 09 '15 at 11:25
1 Answers
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

guillaume girod-vitouchkina
- 3,061
- 1
- 10
- 26