首先,大家在学习的过程中记得大概的看下源码:源码在: 安装目录\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