在 Java 中使用写字板打开文件

Open file with WordPad in Java

我想在 WordPad 中打开一个文件。我试过以下方法:

Desktop.getDesktop().edit(fileName);

但是文件以 Notepad 打开。如何强制写字板打开文件?

Desktop.getDesktop().edit(fileName); 默认打开记事本。要实现你想要的,你需要使用 ProcessBuilder:

注意: 此代码可在我的计算机上使用 Windows 7. 您必须使用 wordpad.exe 的路径才能使它有效,并相应地调整 fileName

// get filename
String fileName = "C:\tmp\Q37545784.txt";

// get executable of wordpad
String wordPadExecutable = "C:\Program Files (x86)\Windows NT\Accessories\wordpad.exe";

// create a process builder that executes wordpad and passes filename as parameter
ProcessBuilder pb = new ProcessBuilder(wordPadExecutable, fileName);

// start
pb.start();