2015-11-06 123 views
0

我是android開發新手。我試圖在Android設備上用數字編寫一個文件。我可以將文件存儲在內部存儲器中,但目前我無法在此文件上寫入。Android內部存儲文件寫入無法正常工作

這是我的代碼。 有任何建議透露給我。

 int count=0; 
     FileOutputStream outputStream; 
     BufferedReader input=null; 
     File file=new File(getCacheDir(),"example.txt"); 
     if(file.exists()==true){ 
      try { 
       input = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 
       String line; 
       StringBuilder builder=new StringBuilder(); 
       while((line=input.readLine())!=null){ 
        builder.append(line); 
       } 
       String g=builder.toString(); 
       Toast.makeText(getApplicationContext(),"Count - " + g,Toast.LENGTH_LONG).show(); 
      }catch (IOException e){ 

      } 
     }else { 
      try { 
       count=count+1; 
       outputStream = new FileOutputStream(file); 
       outputStream.write(count); 
       outputStream.close(); 
       Toast.makeText(getApplicationContext(),"File - "+file.getPath().toString(),Toast.LENGTH_LONG).show(); 
      }catch (IOException e){ 
       Toast.makeText(getApplicationContext(),"Error creating this file",Toast.LENGTH_LONG).show(); 
      } 
     } 

感謝

+0

@ChiragSavsani謝謝。 –

+0

現在工作嗎? –

+0

@ChiragSavsani - 仍然不起作用 –

回答

2

加入這一行AndroidManifest.xml檔案

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

沒有任何改變。 –

+0

然後在這裏發佈你的logcat。 –

+0

其實我正在開發一個項目,所以我現在不能發佈我的logcat。 –

0

謝謝大家,我發現我的問題的解決方案。我可以使用Sting而不是整數來完成上述工作。再見,再見。