2013-04-06 4 views
1

Eclipse에서 프로젝트에서 입력하려는 파일을 마우스 오른쪽 버튼으로 클릭하면 두 가지가 표시됩니다. 경로 : /SWT/src/data.txt 위치 : C : \ Users \ Yoshikawa \ workspace \ SWT \ src \ data.txt파일 경로를 사용하여 파일 위치를 얻는 방법은 무엇입니까?

위치 (C : \ Users \ Yoshikawa \ workspace \ SWT \ src \ data.txt)를 얻을 수있는 방법은 경로 (/ SWT/src/data .txt)?

Ex.

public String ReadFile(String file_path) { 
     //Search current project directory 

      return file_location 
    } 
+0

에 대한 java.io.File의 API는 어쩌면이 질문은 도움이 될 것입니다 참조 : http://stackoverflow.com/ 질문/11692636/how-to-get-eclipse-workspace-location-uri – Ritesh

답변

3
public String readFile(String file_path){ 
    File f = new File(file_path); 
    return f.getAbsolutePath(); 
} 
0

File.getAbsolutePath 또는 File.getCanonicalPath를 사용할 수 있습니다. 그들은, 동일하지 않습니다 차이를 볼

String relativePath = "../1.txt"; 
    System.out.println(new File(relativePath).getCanonicalPath()); 
    System.out.println(new File(relativePath).getAbsolutePath()); 

출력

D:\workspace1\1.txt 
D:\workspace1\x\..\1.txt 

자세한

관련 문제