2012-10-23 112 views
1

我正在嘗試編寫一個簡單的程序,將文件寫入外部存儲並從中讀取數據。以下是對問題的描述:
1.文件名=「hello_files.txt」包含文本「hello world」存儲在/sdcard中。
2.一旦寫入文件的數據被寫入,文件可以用adb看到。
3.然而,一旦應用程序被加載到手機上,並用第三方文件瀏覽器應用程序查看,似乎沒有文本寫入文件hello_files.txt.It似乎有問題的寫我的程序的一部分,但我無法弄清楚什麼。我在下面給出了代碼的兩個部分,用於寫作和閱讀。無法寫入或讀取外部存儲中的文件 - Android

從文件中讀取的代碼:

public void readFromFile() { 

      //Step 1. Check if the storage is available. 
      boolean mStorageAvailable; 
      boolean mStorageReadWrite; 

      String state = Environment.MEDIA_MOUNTED; 
      Log.v(this.toString(), "State of media = " + state); 

      if(Environment.MEDIA_MOUNTED.equals(state)) { 
       Log.v(this.toString(), "Media is mounted."); 
       mStorageAvailable = true; 
       mStorageReadWrite = true; 

       File dir = Environment.getExternalStorageDirectory(); 
       String fileName = dir + "/" + file; 
       Log.v(this.toString(), "Reading from file = " + fileName); 

       File f = new File(dir, file); 

       FileReader fr = null; 
       try { 
        fr = new FileReader(f); 
        Log.v(this.toString(), "Wrapping a buffered reader around file reader."); 
        BufferedReader bufRead = new BufferedReader(fr, 8192); 
        //Log.v(this.toString(), bufRead.toString()); 
        String line; 
        try { 
         line = bufRead.readLine(); 
         Log.v(this.toString(), "Line read = " + line); 
         Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         Log.v(this.toString(), "IOException found in reading line from file."); 
        } 
       } catch (FileNotFoundException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
        Log.v(this.toString(), "File not found for reading."); 
       } 
      } 
} 

用於寫文件的代碼是:

public void writeToFile() { 

     //Step 2. Check if external storage is mounted. 
     boolean mStorageExists; 
     boolean mStorageReadWrite; 

     String state = Environment.getExternalStorageState(); 
     Log.v(this.toString(), "External media mounted state = " + state); 

     if(Environment.MEDIA_MOUNTED.equals(state)) { 
      //media is mounted. 
      //Mounted storage is read/write. 
      //Write to the file. 

      mStorageExists = true; 
      mStorageReadWrite = true; 

      Log.v(this.toString(), "Going to write to a file."); 

      //get directory. 
      File dir = Environment.getExternalStorageDirectory(); 
      Log.v(this.toString(), "Root directory = " + dir.getAbsolutePath()); 
      String fileName = dir.getAbsolutePath() + "/" + file; 

      Log.v(this.toString(), "File to write to is present at " + fileName); 

      File f = new File(dir, file); 

      FileOutputStream fos; 
      try { 
       fos = new FileOutputStream(f); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
       Log.v(this.toString(), "File not found."); 
      } 
      try { 
       fos = openFileOutput(file, MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE); 
       Log.v(this.toString(), "Bytes of the string = " + str.getBytes().toString()); 
       fos.write(str.getBytes()); 
       //Log.v(this.toString(), "Local path = "); 
       fos.close(); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       Log.v(this.toString(), "File not found!!"); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       Log.v(this.toString(), "IO Exception."); 
       e.printStackTrace(); 
      } 
     } else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
      //media is mounted but read only. 
      mStorageExists = true; 
      mStorageReadWrite = false; 
      Log.v(this.toString(), "The media is read-only mounted."); 
     } else if((Environment.MEDIA_NOFS.equals(state)) || (Environment.MEDIA_REMOVED.equals(state)) || (Environment.MEDIA_UNMOUNTED.equals(state))) { 
      mStorageExists = false; 
      mStorageReadWrite = false; 
      Log.v(this.toString(), "There is some problem with the media."); 
     } 
    } 

日誌(從Log詳細標籤):

10-23 10:26:19.444: V/[email protected](466): Read from file button clicked. 
10-23 10:26:19.444: V/[email protected](466): State of media = mounted 
10-23 10:26:19.444: V/[email protected](466): Media is mounted. 
10-23 10:26:19.444: V/[email protected](466): Reading from file = /sdcard/hello_files.txt 
10-23 10:26:19.453: V/[email protected](466): Wrapping a buffered reader around file reader. 
10-23 10:26:19.453: V/[email protected](466): Line read = null 
+0

請登錄後發表 –

+0

。文件hello_files.txt只包含一行:hello world – Sriram

+0

@SardorDushamov:任何幫助都是最受歡迎的..。 – Sriram

回答

0

除了包含訪問外部存儲的權限外,還有以下用於編寫g和從一個文件讀取工作。

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

寫入文件:

public void writeToFile() { 

     //Step 2. Check if external storage is mounted. 
     boolean mStorageExists; 
     boolean mStorageReadWrite; 

     String state = Environment.getExternalStorageState(); 
     Log.v(this.toString(), "External media mounted state = " + state); 

     if(Environment.MEDIA_MOUNTED.equals(state)) { 
      //media is mounted. 
      //Mounted storage is read/write. 
      //Write to the file. 

      mStorageExists = true; 
      mStorageReadWrite = true; 

      Log.v(this.toString(), "Going to write to a file."); 

      //get directory. 
      File dir = Environment.getExternalStorageDirectory(); 
      Log.v(this.toString(), "Root directory = " + dir.getAbsolutePath()); 

      String file_to_write = getFileToWrite(dir); //this returns the exact name of the file. 

      String fileName = dir.getAbsolutePath() + "/" + file_to_write; 

      Log.v(this.toString(), "File to write to is present at " + file_to_write); 

      File f = new File(dir, file_to_write); 
      FileWriter fw = null; 
      try { 
       fw = new FileWriter(f); 
       fw.write(str); 
       fw.close(); 
      } catch (IOException e2) { 
       // TODO Auto-generated catch block 
       Log.v(this.toString(), "IOException caught in initializing filewriter."); 
       e2.printStackTrace(); 
      } 
     } else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
      //media is mounted but read only. 
      mStorageExists = true; 
      mStorageReadWrite = false; 
      Log.v(this.toString(), "The media is read-only mounted."); 
     } else if((Environment.MEDIA_NOFS.equals(state)) || (Environment.MEDIA_REMOVED.equals(state)) || (Environment.MEDIA_UNMOUNTED.equals(state))) { 
      mStorageExists = false; 
      mStorageReadWrite = false; 
      Log.v(this.toString(), "There is some problem with the media."); 
     } 
    } 

和功能從文件中讀取:

public void readFromFile() { 

     //Step 1. Check if the storage is available. 
     boolean mStorageAvailable; 
     boolean mStorageReadWrite; 

     String state = Environment.MEDIA_MOUNTED; 
     Log.v(this.toString(), "State of media = " + state); 

     if(Environment.MEDIA_MOUNTED.equals(state)) { 
      Log.v(this.toString(), "Media is mounted."); 
      mStorageAvailable = true; 
      mStorageReadWrite = true; 

      File dir = Environment.getExternalStorageDirectory(); 
      String file_to_read_from = getFileToRead(dir); 
      String fileName = dir + "/" + file_to_read_from; 
      Log.v(this.toString(), "Reading from file = " + fileName); 

      File f = new File(dir, file_to_read_from); 
      //Log.v(this.toString(), "Read from file = " + f.canRead() + " " + f.isFile() + " " + f.exists() + " " + f.length() + " " + f.lastModified()); 

      try { 
       FileReader fis = new FileReader(f); 
       Log.v(this.toString(), "Wrapping a buffered reader around file reader."); 
       BufferedReader bufRead = new BufferedReader(fis, 100); 
       String line; 
       try { 
        line = bufRead.readLine(); 
        Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show(); 
        Log.v(this.toString(), "Line read = " + line); 
        while(line != null) { 
         Log.v(this.toString(), "Line read = " + line); 
         Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show(); 
         line = bufRead.readLine(); 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        Log.v(this.toString(), "IOException found in reading line from file."); 
       } 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
       Log.v(this.toString(), "File not found for reading."); 
      } 
     } 
    } 
+0

。我很欣賞後續行動。我會嘗試你發佈的代碼並讓你知道。 –

相關問題