1

ファイルを外部ストレージに書き込み、そこから読み取る簡単なプログラムを作成しようとしています。問題の説明は次のとおり
です/sdcard
2. ファイルにデータが書き込まれると、ファイルは で表示されますadb
3.しかし、アプリを携帯にロードしてサードパーティのファイルエクスプローラーアプリで見ると、hello_files.txtファイルにテキストが書き込まれていないようです。書き込み部分に問題があるようです私のプログラムの、しかし、私は何を理解することはできません。以下に、書き込みと読み取りの両方のコード部分を示します。

ファイルから読み取るためのコード:

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

ログ ( Logverbose タグから):

10-23 10:26:19.444: V/com.sriram.fileops.MainActivity$2@44e95cc0(466): Read from file button clicked.
10-23 10:26:19.444: V/com.sriram.fileops.MainActivity@44e90428(466): State of media = mounted
10-23 10:26:19.444: V/com.sriram.fileops.MainActivity@44e90428(466): Media is mounted.
10-23 10:26:19.444: V/com.sriram.fileops.MainActivity@44e90428(466): Reading from file = /sdcard/hello_files.txt
10-23 10:26:19.453: V/com.sriram.fileops.MainActivity@44e90428(466): Wrapping a buffered reader around file reader.
10-23 10:26:19.453: V/com.sriram.fileops.MainActivity@44e90428(466): Line read = null
4

1 に答える 1