文章标题

首先,大家在学习的过程中记得大概的看下源码:源码在:     安装目录\sdk\sources\android-18\android\app\ContextImpl.java

@Override
public FileOutputStream openFileOutput(String name, int mode)
throws FileNotFoundException {
final boolean append = (mode&MODE_APPEND) != 0;
File f = makeFilename(getFilesDir(), name);
try {
FileOutputStream fos = new FileOutputStream(f, append);
setFilePermissionsFromMode(f.getPath(), mode, 0);
return fos;
} catch (FileNotFoundException e) {
}

    File parent = f.getParentFile();
    parent.mkdir();
    FileUtils.setPermissions(
        parent.getPath(),
        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
        -1, -1);
    FileOutputStream fos = new FileOutputStream(f, append);
    setFilePermissionsFromMode(f.getPath(), mode, 0);
    return fos;
}

下面是具体的使用(借鉴:http://blog.csdn.net/janronehoo/article/details/6960137
package demo.filerw.service;

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

import android.content.Context;
import android.os.Environment;

/**
* 文件操作类
* @author janrone
* @website http://hujl.sinaapp.com
*/
public class FileService {

private Context context;  

public FileService(Context context) {  
    this.context = context;  
}  

//存储数据到文件  
public void saveName(String name) throws Exception{  
    //context.getFilesDir();// 得到存放文件的系统目录 /data/data/<package name>/files  
    //context.getCacheDir(); //缓存目录  /data/data/<package name>/cache  
    FileOutputStream outputStream=context.openFileOutput("deomfilerw.txt", Context.MODE_APPEND);  
    outputStream.write(name.getBytes());  
    outputStream.close();  
}  
//存储数据到sdcard  
public void saveNameToSDCard(String name) throws Exception{  
    Environment.getExternalStorageDirectory(); //得到sdcard目录  
    File file=new File("/sdcard","demosdcard.txt");  
    FileOutputStream outputStream=new FileOutputStream(file);  
    outputStream.write(name.getBytes());  
    outputStream.close();  
}  

// 读取数据  
public String getName() throws Exception{  
    FileInputStream inputStream=context.openFileInput("deomfilerw.txt");  
    ByteArrayOutputStream outStream=new ByteArrayOutputStream();  
    byte[] buffer=new byte[1024];  
    int len=0;  
    while ((len=inputStream.read(buffer))!=-1){  
        outStream.write(buffer, 0, len);  
    }  
    outStream.close();  
    byte[] data=outStream.toByteArray();  
    String name=new String(data);  
    return name;  
}  

}

事件监听类可以放到外边
[java] view plaincopy
package demo.filerw.clicklistener;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import demo.filerw.R;
import demo.filerw.service.FileService;

public class ClickListener implements OnClickListener {

private FileService fileService;  
private EditText editText;  
private TextView show_text;  
private Context context;  

public ClickListener(Context context) {  
    this.context=context;  
    fileService = new FileService(context);  
    Activity activity = (Activity) context;  
    editText = (EditText) activity.findViewById(R.id.edit_name);  
    show_text = (TextView) activity.findViewById(R.id.show_text);  
}  

@Override  
public void onClick(View v) {  
    // TODO Auto-generated method stub  
    switch (v.getId()) {  
    case R.id.ok_button:  
        String name=editText.getText().toString();  
        try {  
            fileService.saveName(name);  
            Toast.makeText(context, R.string.ok_succee, Toast.LENGTH_SHORT).show();  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            //Toast.makeText(contex, R.string.ok_error, Toast.LENGTH_SHORT).show();  
        }  
        break;  

    case R.id.read_button:  
        try {  
            String name1 =fileService.getName();  
            show_text.setText(name1);  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            Toast.makeText(context, R.string.read_error, Toast.LENGTH_SHORT).show();  
        }  
        break;  
    }  
}  

}
Activity
[java] view plaincopy
package demo.filerw;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import demo.filerw.clicklistener.ClickListener;

public class FileRWActivity extends Activity {

private Button ok_button;  
private Button read_button;  


/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    ok_button = (Button) findViewById(R.id.ok_button);  
    read_button = (Button) findViewById(R.id.read_button);  

    ok_button.setOnClickListener(new ClickListener(this));  
    read_button.setOnClickListener(new ClickListener(this));  

}  

}

main.xml
[java] view plaincopy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值