简体   繁体   中英

Java - How to edit a file using a specific program?

I know that we can open the edit for a image file etc using java, and here is my example:

File file = new File("c:\\Users\\Aleksa\\Desktop\\Logo.png");
Desktop.getDesktop().edit(file);

Now, I want to know, how do I open a specific app to edit that file. For example, Java checks if I have "Abdobe PhotoShop", and if I do, opens the file in Photo Shop, not in paint, like it does. And way to open a specific editor to edit a file?

-Thanks for helping.

from https://docs.oracle.com/javase/tutorial/uiswing/misc/desktop.html :

"The Desktop API uses the host operating system's file associations to launch applications associated with specific file types. For example, if OpenDocument text (.odt) file extensions are associated with the OpenOffice Writer application, a Java application could launch OpenOffice Writer to open, edit, or even print files with that association. Depending on the host system, different applications may be associated with different actions. For example, if a particular file cannot be printed, check first whether its extension has a printing association on the given operating system."

I don't think you can change such association from Java, and if you could it would probably affect your OS too.

TL;DR: if you want to open with the default OS app, use Desktop.open() . If you want to open with a custom editor, invoke the editor explicitly, for example with Runtime.exec() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM