2015-02-06 3 views
0

이 질문은 중복 된 것일 수 있습니다. 나는 그것을 유감스럽게 생각한다. 비슷한 게시물을 여러 번 보았지만 일을 끝내지 못했습니다.내부 메모리에 디렉토리를 만들고 파일을 저장하십시오.

내 응용 프로그램에서 Json 데이터를 .txt 파일에 저장하고 내부 메모리의 특정 폴더에 저장하려고합니다. 또는 파일을 Android/data 폴더에 직접 저장하는 것도 괜찮습니다.

다음 코드를 사용하여 저장했지만 원하는 파일을 찾을 수 없습니다. 아무도 내 코드와 teh 문제가 무엇인지 말해 줄 수 있고 어떻게 그것을 수정할 수 있습니까 ??

String fname, fcontent; 
Context context; 
ProgressDialog pDialog; 

public SaveIntoFile(String fileName, String jsonStr, Context context, ProgressDialog pDialog) { 
    this.fname = fileName; 
    this.context = context; 
    this.fcontent = jsonStr; 
    this.pDialog = pDialog; 
} 

public void write() { 
    try { 

     File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE); 
     File file = new File(mydir, fname+".txt"); 

     if (file.exists()) 
      file.delete(); 
     // If file does not exists, then create it 
     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write(fcontent); 
     bw.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 

    } 
} 

public String read() throws IOException { 
    BufferedReader br = null; 
    String response = null; 

    StringBuffer output = new StringBuffer(); 

    File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE); 
    File file = new File(mydir, fname+".txt"); 

    if (!file.exists()) { 

     if (pDialog.isShowing()) 
      pDialog.dismiss(); 

     return null; 
    } else { 
     br = new BufferedReader(new FileReader(file)); 
     String line = ""; 
     while ((line = br.readLine()) != null) { 
      output.append(line + "\n"); 
     } 
     response = output.toString(); 

     br.close(); 

     return response; 
    } 

} 
+0

오류가 있습니까? logcat에 –

+0

매니페스트 파일에 읽기/쓰기 권한이 있습니까? – sUndeep

+0

@RandykaYudhistira logcat에 오류가 표시되지 않습니다. 앱은 잘 실행되지만 파일을 저장하지는 않습니다. –

답변

0

다음은 내부 메모리에 txt 파일을 저장하는 코드입니다. 파일을 보려면. > 파일 탐색기 - - 당신은 에뮬레이터에서 다음 고토 DDMS 테스트 응용 프로그램을 실행해야> app_NepalJapan - 당신의 활동> hello.txt

public static String write(Context context, String strfilename) { 

     String internalMemoryPath = ""; 

     try { 

      File InternalMemoryRoot = context.getDir("NepalJapan", Context.MODE_PRIVATE); 
      final File folderPath = new File(InternalMemoryRoot+""); 

      Log.v("filePath","filePath: "+folderPath); 

      if(!folderPath.exists() && !folderPath.isDirectory()){ 
       folderPath.mkdirs(); 
      } 

      String filePath = folderPath +"/"+ strfilename; 
      Log.v("filePath","filePath: "+filePath); 



      File file = new File(filePath); 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      // create a buffer... 
      byte[] buffer = new byte[16384]; 
      int bufferLength = 1; 
      fileOutput.write(buffer, 0, bufferLength); 
      fileOutput.close(); 

      internalMemoryPath = filePath; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return internalMemoryPath; 
    } 

통화 방법 : YourStaticUtilClass.write(context, "hello.txt");

여전히 어려움을 찾을 경우, 그럼 알려줘.

관련 문제