您的位置:首页 > 其它

最佳拍档之内部存储和外部存储(sdcard)

2015-08-21 22:31 465 查看
大家晚上好,今天如期而至,我开始分享学到的好东西了,给大家讲的是最佳拍档----内部存储和外部存储,其中外部储存值得是sd卡存储,这两种存储文件方式是很常用到,有个优点就是它们两个都是私有的,安全性高,当你卸载一个app时,同样也会把存入的文件删除掉。好了,废话不多说,直接上运行界面。



界面就不多说了,很简单,就是用线性布局设置,然后设置需要id属性的控件就OK了。我做这个用到了两个java文件做的,MainActivity.java和fileMethod.java业务层方法。

2.MainActivity.java

private EditText body_et;

private EditText body_et2;

private Context context;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 初始化

body_et = (EditText) findViewById(R.id.body_et);

body_et2 = (EditText) findViewById(R.id.body_et2);

context = this;

}

/**

* 写内存按钮事件

*/

public void wirteFile(View v) {

try {

//获取输入框输入值

String edit = body_et.getText().toString();

//调用fileMethod方法,因为fileMethod方法用了构造方法,所以这里要new出来,把edit参数传过去

fileMethod fm = new fileMethod(context);

fm.wirteFileMethod(edit);

//吐司

Toast.makeText(context, "写入内部存储成功", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 读内存文件的数据

*/

public void readFile(View v) {

//调用方法

fileMethod fm = new fileMethod(context);

try {

//调用方法有return值,这里可以打得返回的数据

String data = fm.readFileMethod();

//在显示框显示数据,setText

body_et2.setText(data);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 写数据到sdcard

*/

public void writeSdcard(View v) {

String edit = body_et.getText().toString();

//调用方法,把edit参数传过去

fileMethod fm = new fileMethod(context);

try {

fm.writeSdcardMethod(edit);

Toast.makeText(context, "写入sdcard成功", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 读sdcard数据

*/

public void readSdcard(View v) {

fileMethod fm = new fileMethod(context);

try {

String data = fm.readSdcardMethod();

body_et2.setText(data);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

2.fileMethod.java业务层方法

public class fileMethod {

private Context context;

/**

* 构造方法,初始化context上下文,解析一下上下文,上下文是一个环境,里面有变量还有数据,还有很多其他的,它同时也是一个类,有很多方法,比如文件夹的路径,文件夹资源.....

*/

public fileMethod(Context context) {

this.context = context;

}

/**

* 写入内存方法

*/

public void wirteFileMethod(String edit) throws Exception {

//输出流写入数据,用openFileOutput,这就要contex获取其方法,参数一:生成文件的命名,参数二:文件访问的模式,这里是私有

FileOutputStream openFileOutput = context.openFileOutput("huanxi.txt",

context.MODE_PRIVATE);

//写入数据,edit.getByites()转换为byte类型

openFileOutput.write(edit.getBytes());

//关闭流

openFileOutput.close();

}

/**

* 读取文件的方法

*/

public String readFileMethod() throws Exception {

//构建File对象,参数一:文件的路径,默认为data/data/<包名>/files,参数二:文件名

File flie = new File(context.getFilesDir(), "huanxi.txt");

//输入流读取文件

FileInputStream fis = new FileInputStream(flie);

//构建BuffeedReader对象

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

//获取一行的字符型

String str = br.readLine();

fis.close();

//把数据返回出去

return str;

}

/**

* 写数据到外部内存的步骤

1.检测外部内存sdcard是否是挂载状态

2.设置好存放路径

3.开辟输出流,写入数据

4.设置权限

*/

public void writeSdcardMethod(String edit) throws Exception {

//检测sdcard是否是挂载状态

if (Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

//设置文件存放路径

File rootPath = Environment

.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);

File file = new File(rootPath, "huanxi.txt");

//创建输出流,写入数据

FileOutputStream fos = new FileOutputStream(file);

fos.write(edit.getBytes());

fos.close();

}

}

/**

*

*读sdcards数据

*/

public String readSdcardMethod() throws Exception {

//找到存放文件的路径

File rootPath = Environment

.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);

File file = new File(rootPath, "huanxi.txt");

//输入流读取数据

FileInputStream fis = new FileInputStream(file);

//创建BufferedReader对象

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

//读取一行的数据

String str = br.readLine();

//读取每一行的数据

// ByteArrayOutputStream bai=new ByteArrayOutputStream();

// byte[]buf=new byte[2048];

// int len=0;

// while((len=fis.read(buf))>0){

// bai.write(buf, 0, len);

// }

//返回值

return str;

}

}

3.AndroidManifest.xml配置文件

//写入内存卡和是否是挂载状态的配置

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

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

今天两种存储文件的方法就讲到这了,希望友友看完之后能有一点点收获就值得,我辛苦的打字了,好,今天就到这里了,梦想在靠近,你努力了吗?晚安。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: