Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

FileInputStream, FileOutputStream을 이용하여 SDCard에 파일 읽고 쓰기. 본문

안드로이드/File

FileInputStream, FileOutputStream을 이용하여 SDCard에 파일 읽고 쓰기.

설.현.아빠 2011. 2. 11. 10:22



package exam.SDCard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SDCard extends Activity {
    /** Called when the activity is first created. */
    EditText mEdit;
    String mSdPath;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mEdit = (EditText)findViewById(R.id.edittext);
        findViewById(R.id.test).setOnClickListener(mClickListener);
        findViewById(R.id.save).setOnClickListener(mClickListener);
        findViewById(R.id.load).setOnClickListener(mClickListener);
        
        String ext = Environment.getExternalStorageState();
        if(ext.equals(Environment.MEDIA_MOUNTED)) {
            mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
            mSdPath = Environment.MEDIA_UNMOUNTED;
        }
    }
    
    Button.OnClickListener mClickListener = new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()) {
            case R.id.test :
                String rootdir = Environment.getRootDirectory().getAbsolutePath();
                String datadir = Environment.getDataDirectory().getAbsolutePath();
                String cachedir =
                    Environment.getDownloadCacheDirectory().getAbsolutePath();
                mEdit.setText(String.format("ext = %s\nroot=%s\ndata=%s\ncache=%s", mSdPath, rootdir, datadir, cachedir));
                break;
            case R.id.save :
                File dir = new File(mSdPath + "/dir");
                dir.mkdir();
                File file = new File(mSdPath + "/dir/file.txt");
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    String str = "This file exists in SDcard";
                    fos.write(str.getBytes());
                    fos.close();
                    mEdit.setText("write success");
                }
                catch (FileNotFoundException e) {
                    mEdit.setText("Security Exception");
                }
                catch (Exception e) {
                    mEdit.setText(e.getMessage());
                }
                break;
            case R.id.load :
                try {
                    FileInputStream fis = new FileInputStream(mSdPath + "/dir/file.txt");
                    byte[] data = new byte[fis.available()];
                    while(fis.read(data) != -1) {;}
                    fis.close();
                    mEdit.setText(new String(data));
                } catch (FileNotFoundException e) {
                    mEdit.setText("File Not Found");
                }
                catch (Exception e) {;}
                break;
            }
        }
    };
}

 

http://seedtale.kr/1347

Comments