2011-04-12 3 views
0

이 코드를 사용하여 sdcard의 루트에 파일을 만들었지 만, "fileDir + fileName"대신에 디렉토리를 만들었습니다!대신에 생성 된 디렉토리를 생성하고 싶습니다!

File file = new File(sdCard.getAbsolutePath(), fileDir+fileName); 
    if (!file.mkdirs()) { 

     Log.d("ERROR", "Create dir in sdcard failed"); 

     return; 
    } 
     OutputStream out = new FileOutputStream(file); 
.............................. 

감사합니다.

+0

정확히 file.mkdirs()와 관련이 있기 때문에? –

답변

1

file.mkdirs()는 if 절에 디렉토리 구조를 생성합니다. file.canWrite()로 작성할 수 있는지 테스트 할 수 있습니다.

설명서는 here입니다. 당신은이 디렉토리를 생성하는 말 때문에

, 그것은 사실 반환해야하지만, 중요한 부분은 파일 [참고)을 (:) 여기

2

file.mkdirs을 보여 디렉토리를 생성하지되지 않습니다 http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdir()합니다. 이것은 디렉토리를 생성하는 코드입니다. 다음과 같은 코드를 사용해야합니다.

String destination = currentdir + "test.txt"; 
     File fileCon = new File(destination); 
     if(! fileCon.exists()){ 
       fileCon.createNewFile(); 
      } 

다른 방법이 시도,

 String destination = currentdir + "test.txt"; 


// Open output stream 
      FileOutputStream fOut = new FileOutputStream(destination); 

      fOut.write("Test".getBytes()); 

      // Close output stream 
      fOut.flush(); 
      fOut.close(); 
+0

maked this : String destination = sdCard.getAbsolutePath() + File.separator + fileDir + fileName; 파일 fileCon = 새 파일 (대상); if (! fileCon.exists()) { fileCon.createNewFile(); } 항상 같은 문제가 발생합니다. – androniennn

+0

FileOutputStream을 이전 버전으로 사용해보십시오. // 출력 스트림 열기 FileOutputStream fOut = new FileOutputStream (path + fname); fOut.write ("Test".getBytes()); // 출력 스트림을 닫습니다. fOut.flush(); fOut.close(); – Priyank

+0

이렇게 나는 파일 안에 "Test"문자열을 쓰고 있지 않습니까? : \ – androniennn

0

당신이 file.mkdirs을 기대하는 경우() 파일에 대한 디렉터리를 만들려면, 당신은) (file.getParentFile에 약간 조정할 필요가있다. mkdirs() 물론

이는

,691, 시도/캐치 절에 싸여 getParentFile()이 도움이 널 (null)

희망을 반환하는 경우에해야

Phil Lello

관련 문제