2009-12-09 4 views

답변

3
Runtime.getRuntime().exec("notepad c:/asd.txt"); 

여기에서 c:/asd.txt은 텍스트 파일의 전체 경로입니다. /이 작동하지 않는 경우 대신 \\을 사용하십시오.

3

사용 ProcessBuilder를 클래스 당신이 당신의 OS에 .txt 확장명을 등록하고 텍스트 파일이 이미 당신도

Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","text.txt"}); 

장점이 소요되는 작업을 수행 할 수있는 경우

Process p = new ProcessBuilder("notepad", "file.txt").start(); 
3

.txt와 관련된 프로그램, notepad.exe와는 다른 것일 수 있습니다. 자바 1.6, .txt를 사용하는 것은 메모장에 등록되고 바탕 화면이 지원되는 경우

+0

'java.awt.Desktop.open'을 사용하면 아마도 그렇게하는 것이 훨씬 더 좋습니다. –

10

당신은 java.awt.Desktop를 사용할 수 있습니다

if (!Desktop.isDesktopSupported()) { 
     System.err.println("Desktop not supported"); 
     // use alternative (Runtime.exec) 
     return; 
    } 

    Desktop desktop = Desktop.getDesktop(); 
    if (!desktop.isSupported(Desktop.Action.EDIT)) { 
     System.err.println("EDIT not supported"); 
     // use alternative (Runtime.exec) 
     return; 
    } 

    try { 
     desktop.edit(new File("test.txt")); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

이 방법 당신은 더 OS 독립적 인 방법으로/편집 파일을 열 수 있습니다.

관련 문제