您的位置:首页 > 移动开发 > Android开发

Android读写操作之sdcard的读写操作

2015-09-22 20:07 633 查看
今天除了讲了内存的读写操作外,还讲了sdcard的读写操作,这个感觉比内存的读写操作要用的多,因为我们一般都是把

文件存储到sdcard中。好了,我们直接看代码吧。

一定要注意:对于文件的读写操作一定不要忘了加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
下面的就是MainActivity了:

package com.example.file_02;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

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

public class MainActivity extends Activity implements OnClickListener{

private EditText et_read,et_write;
private Button btn_read,btn_write;
//声明一个文件名
final String FILE_NAME = "myFile.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
et_read = (EditText) this.findViewById(R.id.et_read);
et_write = (EditText)this.findViewById(R.id.et_write);
btn_read = (Button)this.findViewById(R.id.btn_read);
btn_write = (Button)this.findViewById(R.id.btn_write);
btn_read.setOnClickListener(this);
btn_write.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_read:
//读取文件中的内容,判断sdcard是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取读取sdcard的路径
File sdcardFile = Environment.getExternalStorageDirectory();
//声明要读取的文件
File file = new File(sdcardFile, FILE_NAME);
//疯狂的读操作
BufferedInputStream bis = null;
try {
//这里我需要注意一下,不论对于读或写都是对于字节的操作,这里我们可以使用BufferedInputStream
//当然也可以使用BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
//这样可以读取一行,大家根据需求来定吧
bis = new BufferedInputStream(new FileInputStream(file));
int len = 0;
String result = null;
byte[] buf = new byte[1024];
while((len = bis.read(buf))!= -1){
result = new String(buf,0,len);
}

et_read.setText(result);
Toast.makeText(MainActivity.this, "文件读取成功", 0).show();
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}finally{
try {
bis.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}
break;
case R.id.btn_write:
//写入文件,判断sdcard是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
BufferedOutputStream bos = null;
//获取写入sdcard的路径
File sdcardFile = Environment.getExternalStorageDirectory();
//获取要写入内容的文件
File file = new File(sdcardFile, FILE_NAME);
//疯狂的读写操作
try {
bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(et_write.getText().toString().getBytes());
Toast.makeText(MainActivity.this, "文件写入成功", 0).show();
} catch (IOException e) {

e.printStackTrace();
}finally{
try {
bos.close();
} catch (IOException e) {

e.printStackTrace();
}
}

}
break;

default:
break;
}
}

}
xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >
<EditText
android:id="@+id/et_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/et_write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
/>
<Button
android:id="@+id/btn_read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取文件"/>
<Button
android:id="@+id/btn_write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入文件"/>

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