android sd卡读写 附源码
将文件存在sd卡中和存在手机中的思路相同就是路径不同,注意看类中的到sd卡路径的注释
package cn.lee.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import cn.lee.Manager.DataManager;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AboutDateActivity extends Activity {
private EditText fileNameEditText;
private EditText fileContentEditText;
private EditText readfileContentEditText;
private Button button;
private Button readButton;
private static final String TAGSTRING = "AboutDateActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.button);
readButton = (Button) this.findViewById(R.id.readButton);
fileNameEditText = (EditText) this.findViewById(R.id.fileName);
fileContentEditText = (EditText) this.findViewById(R.id.fileContent);
readfileContentEditText = (EditText) this
.findViewById(R.id.readfileContent);
// 读取
readButton.setOnClickListener(onClickListener);
// 保存
button.setOnClickListener(onClickListener);
}
/**
* 由于这个内部实现类是一个接口的实例 所以这里把他提取出来 作为一个成员变量 2010-04-18
*/
private View.OnClickListener onClickListener = new View.OnClickListener() {
/**
* 由于button的父类是view 所以这里的onclick的参数传来的view v就可以找到这个button
*/
public void onClick(View v) {
Button button = (Button) v;// 将父类view强转成子类button
int info = R.string.sus;
String fileNameString = fileNameEditText.getText().toString();
/**
* 判断sd卡是否存在 Environment.getExternalStorageState() 得到sd卡当前的状态
*
* getExternalStorageState() returns MEDIA_MOUNTED if the
* media is present and mounted at its mount point with
* read/write access. 如果返回 MEDIA_MOUNTED表示外部存储设备存在。并且有读写的权限(因为sd卡有写保护 如果写保护关闭也是没有权限读写的)
*/
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
if ("".equals(fileNameString)) {
info = R.string.sdNotExit;
} else {
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
try {
/**
* Environment.getExternalStorageDirectory();
* 得到外存储设备的路径
*
*/
File file = new File(Environment.getExternalStorageDirectory(),fileNameString);
switch (button.getId()) {
case R.id.button: {// 保存
String fileContentString = fileContentEditText.getText().toString();
fileOutputStream = new FileOutputStream(file);
DataManager.saveDate(fileOutputStream,fileContentString);
break;
}
case R.id.readButton: {//读取
fileInputStream = new FileInputStream(file);
String contentString = DataManager.readDate(fileInputStream);
readfileContentEditText.setText(contentString);
break;
}
default:
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i(TAGSTRING, e.toString());
info = R.string.infor;
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
Log.i(TAGSTRING, e.toString());
info = R.string.infor;
}
}
}
} else {
info = R.string.infor;
}
Toast.makeText(AboutDateActivity.this, info, 1).show();
}
};
}
好文章,谢谢朋友
好文章,写的不错,赞一个。 :idea: