File 存储(android)

android 中使用文件进行存储程序数据

注意,读写 sdcard 时,需要用到权限,具体代码如下

xml

<?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:layout_width="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="@string/hello" />
    <EditText 
        
android:id="@+id/et"
        android:layout_height
="wrap_content"
        android:layout_width
="fill_parent"
        android:gravity 
="left"
        
/>
    <Button 
        
android:id="@+id/btnr"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="read"
        
/>
    <Button 
        
android:id="@+id/btnw"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="write"
        
/>
    <Button 
        
android:id="@+id/btnsd"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="write to sdcard"
        
/>
    <Button 
        
android:id="@+id/btnwpro"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="write pro"
        
/>
    <Button 
        
android:id="@+id/btnrpro"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="read pro"
        
/>
</LinearLayout>

java 代码

package zziss.android.filetest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

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

public class FiletestActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
    private EditText et;
    private Button   btnr;
    private Button   btnw;
    private Button   btnwsd;
    private Button   btnwpro;
    private Button   btnrpro;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        et = (EditText)this.findViewById(R.id.et);
        btnr = (Button)this.findViewById(R.id.btnr);
        btnw = (Button)this.findViewById(R.id.btnw);
        btnwsd = (Button)this.findViewById(R.id.btnsd);
        btnwpro = (Button)this.findViewById(R.id.btnwpro);
        btnrpro = (Button)this.findViewById(R.id.btnrpro);
        btnw.setOnClickListener(this);
        btnr.setOnClickListener(this);
        btnwsd.setOnClickListener(this);
        btnwpro.setOnClickListener(this);
        btnrpro.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId()==btnw.getId())
        {
            try {
                FileOutputStream fot = openFileOutput("fc.dat",MODE_PRIVATE );
                fot.write(et.getText().toString().getBytes());
                fot.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
                toast.show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
                toast.show();
            }
        }
        
        if (v.getId()==btnr.getId())
        {
            try {
                FileInputStream fot = openFileInput("fc.dat");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] str = new byte[1024];
                int len = -1;
                while ((len=fot.read(str))>-1)
                {
                    baos.write(str,0, len);
                }
                et.setText(baos.toString());
                fot.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (v.getId()==btnwsd.getId())
        {
            // 查找 sdcard 状态
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            {
                
                File ff=this.getFileStreamPath("fc.dat");
                File fpath=Environment.getExternalStorageDirectory();
                File ft = new File(fpath,"fc.dat");
                
                try {
                    FileOutputStream fos = new FileOutputStream(ft);
                    FileInputStream fis  = new FileInputStream(ff);
                    int len=-1;
                    byte[] buf = new byte[1024];
                    while ( (len=fis.read(buf))>-1)
                    {
                        fos.write(buf,0,len);
                    }
                    fos.close();
                    fis.close();
                    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    Toast toast = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
                    toast.show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Toast toast = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        } // end write to sdcard
        
        if (v.getId()==btnwpro.getId())
        {
            Properties pro = new Properties();
            pro.put("text", et.getText().toString());
            
            try {
                FileOutputStream fos = this.openFileOutput("fc.dat",MODE_PRIVATE);
                pro.store(fos, "");
                fos.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
                toast.show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            pro= null;
        } // end write pro 
        if (v.getId()==btnrpro.getId())
        {
            Properties pro = new Properties();
            try {
                FileInputStream fis = this.openFileInput("fc.dat");
                pro.load(fis);
                fis.close();
                et.setText(pro.getProperty("text"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Toast toast = Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT);
                toast.show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } // end read pro 
        
    }
}

androidMain.xml

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

    <uses-sdk android:minSdkVersion="14" />
    <!-- 添加上读写存储卡 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

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

</manifest>
原文地址:https://www.cnblogs.com/zziss/p/2320816.html