2010-12-13 3 views
6

나는이 예외가 (시스템이 지정된 경로를 찾을 수 없습니다) 만들 수 있습니다. 응용 프로그램에는 디렉토리에 대한 전체 권한이 있습니다.FileNotFoundException이

아이디어가 있으십니까?

답변

8

문제는 파일을 쓰는 하위 디렉토리를 만들기 때문입니다. 그래서 현재 C:\example\을 가지고 있고 당신은 쓰기 전에 File#mkdirs()를 호출 할 필요가 C:\example\<date>\<time>\<files>

에 내 파일을 작성합니다.

File file = new File("C:/example/newdir/newdir/filename.ext"); 
file.mkdirs(); 
// ... 
+0

그러면 해결됩니다. 고마워요! – Michael

4

컴퓨터가 옳고 틀렸다고 가정하십시오.

그리고이 시나리오에서는 작성하려는 디렉터리가 종료되지 않거나 이렇게 할 수있는 권한이 없습니다.

  1. 검사가
+0

그렇다고 가정하지 않았습니다. 문제는 파일을 쓰는 하위 디렉토리를 만들기 때문입니다. 그래서 나는 현재 C : \ example \을 가지고 있고 내 파일을 C : \ example \ \

2

코드에서 현재 작업 디렉토리 System.getProperty("user.dir")

  • 디버그 나를 위해 작동합니다. 파일에 텍스트가 표시되도록하려면 writer.close()을 추가해야합니다.

  • +0

    에 쓰고 싶습니다. 존재하는 디렉토리에 글을 쓸 때 좋습니다. 존재하지 않는 하위 디렉토리에 글을 쓰고 있기 때문에 문제가 발견되었습니다. 현재 C : \ example \이 (가) 있고 C : \ example \ 에 파일을 쓰고 싶습니다. \

    1

    새로 만든 파일 및 폴더 경로를 string으로 변환해야합니다.

    File folder = new File("src\\main\\json\\", idNumber); 
        folder.mkdir(); 
    
        if (!folder.exists()) { 
         try { 
          folder.createNewFile(); 
         } catch (IOException ex) { 
          Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex); 
         } 
        } 
        ... 
        ... 
        FileOutputStream output = null; 
         File file; 
         String content = data.toString(); 
    
         try { 
    
          String folder_location = folder.toString() + "\\"; 
          String filename = "CurrentInfo"; 
          file = new File(folder_location + filename.toString() + ".json"); 
          output = new FileOutputStream(file); 
    
          if (!file.exists()) { 
           file.createNewFile(); 
          } 
    
          byte[] content_in_bytes = content.getBytes(); 
    
          output.write(content_in_bytes); 
          output.flush(); 
          output.close(); 
    
         } catch (IOException ex) { 
          Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex); 
         } finally { 
          try { 
           if (output != null) { 
            output.close(); 
           } 
          } catch (IOException e) { 
           Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, e); 
          } 
         } 
    
        } 
    
    관련 문제