프로젝트에서 프로필 경로에 따라 File 객체를 생성하는 방법
2463 단어 io
소스의 주석은 다음과 같습니다.
/**
* Creates a new File
instance by converting the given
* pathname string into an abstract pathname. If the given string is
* the empty string, then the result is the empty abstract pathname.
*
* @param pathname A pathname string
* @throws NullPointerException
* If the pathname
argument is null
*/
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
문자열 파라미터가 빈 문자열이면 빈 추상적인 경로를 되돌려준다는 뜻이다.null이면 바늘을 비우면 이상합니다.
@Test
public void testFilePathName() {
File file = null;
file = new File("");
System.out.println(file.getAbsolutePath());
// D:\workspace\test
file = new File("/");
System.out.println(file.getAbsolutePath());
// D:\
String pathName = "/test/src/main/resources/dd.txt";
file = new File(pathName);
System.out.println(file.getAbsolutePath());
// D:\test\src\main\resources\dd.txt
file = new File("test/src/main/resources/dd.txt");
System.out.println(file.getAbsolutePath());
// D:\workspace\test\test\src\main\resources\dd.txt
file = new File("/src/main/resources/dd.txt");
System.out.println(file.getAbsolutePath());
// D:\src\main\resources\dd.txt
file = new File("src/main/resources/dd.txt");
System.out.println(file.getAbsolutePath());
// D:\workspace\test\src\main\resources\dd.txt
}
문자열 매개변수의 경로 이름이 상대 경로인 경우 문자가 없는 경우 기본적으로 시스템은 사용자의 작업 경로에 따라 상대 경로를 해석합니다.작업 경로란?System.getProperty("user.dir")의 값입니다.간단하게 말하면 이 프로젝트가 있는 경로입니다. 본 프로젝트는 D디스크 워크스페이스 폴더 아래에 있고 프로젝트 이름은test이기 때문에 작업 경로는 D:\워크스페이스\test2입니다.문자열 파라미터가 빈 문자열이면 구조 방법이 작업 경로로 되돌아옵니다.단일 슬래시 "/"인 경우 항목이 있는 디스크의 루트(예: D:\) 경로가 반환됩니다.프로필의 한정된 이름을 전송하면 (오른쪽 단추, Copy Qualified Name, 슬래시로 시작하는 것을 알 수 있습니다) 되돌아오는 경로 이름 문자열에 중복된 항목 이름이 포함되어 있음을 알 수 있습니다./projectName을 제거한 후 전송(지금은 일사봉으로 시작하는 문자열) 되돌아오는 경로가 루트 경로에 전송된 문자열 인자인 것을 발견합니다.이어서 문자열 매개 변수가 시작된 줄을 제거하고 입력한 후 정확한 경로 이름을 되돌려줍니다.따라서 들어오는 문자열 매개 변수는QualifiedName에서/ProjectName/을 제거해야 합니다. 한 줄로 시작하지 않으면 루트 경로로 갑니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
프로젝트에서 프로필 경로에 따라 File 객체를 생성하는 방법File 클래스에서 가장 많이 사용되는 구성 방법은 File(String pathname) 및 File(URI uri) 두 가지가 있는데 그 중에서 가장 자주 사용하는 구성 방법은 File(String pathnam...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.