2015-02-06 72 views
0

此問題可能會重複。我很抱歉。我經歷了一些類似的帖子,但無法在我的工作中完成。在內部存儲器中創建一個目錄並保存文件

在我的應用程序中,我想將Json數據存儲在.txt文件中,並將它們存儲在內部存儲器中的特定文件夾中。或者直接在Android/data文件夾中存儲文件也可以。

我用下面的代碼來做,但我無法找到存儲的文件,因爲我想要的。任何人都可以告訴我什麼是與我的代碼問題,我如何糾正它?

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中可能 – 2015-02-06 06:26:35

+0

你在Manifest文件中有讀/寫權限嗎? – sUndeep 2015-02-06 06:27:13

+0

@RandykaYudhistira它在logcat中顯示沒有錯誤。該應用程序運行良好,但不存儲文件。 – 2015-02-06 06:28:58

回答

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");

如果仍然發現有任何困難,然後讓我知道。

相關問題