내 프로젝트 디렉토리 구조 (Eclipse) :
MyProject/
src/ --> "source directory" on Eclipse's classpath/buildpath
com.me.myapp
Driver
myconfig.txt
에는 Driver
다음 코드가 있습니다.
public class Driver {
public static void main(String[] args) {
InputStream is = ClassLoader.getSystemClassLoader.getResourceAsStream("myconfig.txt");
if(is == null)
System.out.println("input stream is null");
else
System.out.println("input stream is NOT null :-)");
}
}
이것을 실행하면 다음과 같은 콘솔 출력이 나타납니다.
input stream is null
왜? myconfig.txt
잘못된 위치에 배치 했습니까 ? ClassLoader API를 잘못 사용하고 있습니까? 다른 것? 미리 감사드립니다!
동일한 패키지에있는 경우
InputStream is = Driver.class.getResourceAsStream("myconfig.txt");
가지고있는 방식
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("myconfig.txt");
클래스 경로의 루트에서 파일을 찾고 있습니다. 당신은 사용할 수 있습니다
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("com/me/myapp/myconfig.txt");
검색 규칙은의 javadocClassLoader#getResource(String)
및의 javadoc에Class#getResource(String)
설명되어 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다