文件读写

AndroidSDCard操作(文件读写,容量计算):http://zhuyonghui116.blog.hexun.com/56778119_d.html

Android中读写文件:http://blog.csdn.net/cocodehouse/article/details/5974288

增加权限:<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

StringsDStateString=android.os.Environment.getExternalStorageState();

//拥有可读可写权限

if(sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)){

......

}

然后用FileInputStream和FileOutputStream进行读写。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.file.rw"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ReadWriteFilesActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="a.txt" />



    <Button
        android:id="@+id/button1"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:text="Load" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/button2"
        android:layout_width="68dp"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>
package com.file.rw;

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

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

public class ReadWriteFilesActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button1,button2;
	private EditText editText1,editText2;
	private TextView textview;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        editText1 = (EditText)findViewById(R.id.editText1);
        editText2 = (EditText)findViewById(R.id.editText2);
        textview =(TextView)findViewById(R.id.textview);
        //load file from sd card
        button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textview.setText("Load............");
				String sdStatus = android.os.Environment.getExternalStorageState();
				if(sdStatus.equalsIgnoreCase(android.os.Environment.MEDIA_MOUNTED)){
					textview.setText(editText1.getText());
					File rootDir = android.os.Environment.getExternalStorageDirectory();
					File readFile = new File(rootDir.getAbsolutePath()+File.separator+editText1.getText());
					if(!readFile.exists()){
						textview.setText(editText1.getText()+"不存在.");
					}else{
						try {
							FileInputStream in = new FileInputStream(readFile);
							byte[] buffer = new byte[in.available()];
							in.read(buffer);
							in.close();
							editText2.setText(new String(buffer));
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
					}
				}//end of android.os.Environment.MEDIA_MOUNTED
				
			}
		});
        
        button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(editText1.getText()==null||editText1.getText().toString().trim().length()==0){
					textview.setText("第一个输入框没有输入文件名.");
					return;
				}
				if(editText2.getText()==null||editText2.getText().toString().trim().length()==0){
					textview.setText("第而个输入框没有输入文件内容.");
					return;
				}
				String sdStatus = android.os.Environment.getExternalStorageState();
				if(android.os.Environment.MEDIA_MOUNTED.equalsIgnoreCase(sdStatus)){
					File root = android.os.Environment.getExternalStorageDirectory();
					File newFile = new File(root.getAbsolutePath()+File.separator+editText1.getText());
					try {
						if(!newFile.exists()){
							newFile.createNewFile();
						}
						FileOutputStream out = new FileOutputStream(newFile);
						out.write(editText2.getText().toString().getBytes());
						out.close();
						textview.setText("写文件成功.");
					} catch (Exception e) {
						// TODO: handle exception
					}
					
				}
				
			}
		});
        
    }
}

相关推荐