2 回答

TA貢獻1777條經(jīng)驗 獲得超3個贊
微軟視窗語法
import java.awt.Desktop;
import java.io.File;
public class openpic {
public static void main (String args[]) throws Exception
{
// Microsoft Windows syntax
File f = new File ("C:\\Users\\charl\\Desktop\\Computer Science\\JavaProjects\\src\\snp.png");
Desktop d = Desktop.getDesktop();
d.open(f);
System.out.println("imageviewer open;");
}
}

TA貢獻1805條經(jīng)驗 獲得超9個贊
(Ops...修復(fù)答案,在我閱讀代碼上方的文字后)
相對路徑將從您運行程序的目錄開始。也稱為當前工作目錄。
此外,當您使用 Files 時,請嘗試使用 NIO API 和 Path。喜歡:
Path filePath = Paths.get("./snp.png")
使用此 API,您可以使用以下方法檢查工作目錄:
filePath.toAbsolutePath()
// just print it then, or check with a debugger
另外,請注意斜線。
當使用 Windows 和這個斜杠時\,你需要把它們加倍:\\.
另一種選擇是反轉(zhuǎn)它:/。
添加回答
舉報