2017-11-20 2 views
0

ObjectOutputStream을 사용하여 프로젝트의 .iml 파일과 같은 디렉토리에있는 파일에 객체를 씁니다. 파일이 존재합니다. 어떤 이유로 FileInputStream을 사용하여 읽을 수 없습니다. 내가 파일 scene.dodge 프로젝트의 루트 디렉토리에 있는지 확인했습니다IntelliJ IDE - 루트 프로젝트 디렉토리의 FileNotFound 예외

File sceneFile=new File("scene.dodge"); 
FileInputStream fin=new FileInputStream(sceneFile); 
ObjectInputStream in=new ObjectInputStream(fin); 
return (Scene)(in.readObject()); 

: 나는 다음과 같이 2 내 코드는 라인에서 FileNotFound 예외를받을. 어떤 제안? 나는 컴파일러 리소스 패턴을 엉망으로 만들려고했지만 아무 것도 할 수 있는지 전혀 모른다. 나는 단순히 난처한 사람입니다.

답변

0

현재 디렉토리에서 파일을 읽으려면 ClassLoader# getResource method을 사용해야합니다. 하중이

URL dodgeSceneURL = classLoader.getResource("scene.dodge"); 
File sceneFile = new File(dodgeSceneURL.toURI()); 
FileInputStream fin = new FileInputStream(sceneFile); 
+0

같은 파일이 내가 시도 :. URL sceneURL = Scene.class.getClassLoader()의 getResource (경로); 어떤 이유로 든 여전히 nullpointerexception이 발생했습니다 – TinFellow

+0

classLoader는 어디에서 인스턴스화됩니까? 나는이 사실을 이해하지 못한다는 것을 두려워합니다. – TinFellow

+0

@TinFellow'this.getClass(). getClassLoader()'는 classLoader를 얻는 방법과 관련이 없기 때문에 전체 코드를 게시하지 않았습니다. –

관련 문제