android读写SD卡上的文件

时间:2022-09-04 19:06:07

androidManifest.xml的配置文件:

!-- 在SD卡中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 向SD卡写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXITERNAL_STORAGE"/>

package com.example.sdcarddemo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.RandomAccess;

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

public class MainActivity extends Activity {
final String FILE_NAME ="/SDCardDemo.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取两个按钮
Button readButton = (Button) findViewById(R.id.readbtn);
Button writeButton = (Button) findViewById(R.id.writebtn);
//获取两个文本框
final EditText edit1 = (EditText) findViewById(R.id.edit1);
final EditText edit2 = (EditText) findViewById(R.id.edit2);
//为两个按钮绑定事件监听器
writeButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//将edit1中的内容写入文件中
write(edit1.getText().toString());
edit1.setText("");
}
});
readButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//读取指定文件中的内容,并显示出来
edit2.setText(read());
}
});
}
/**
* Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
* 如果手机已插入SD卡,且应用程序具有读写SD卡的能力,上面语句返true
* */
private String read(){
try {
//如果手机插入SD卡,而且应用程序具有访问SD的权限
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SD卡对应的储存目录
File sdCardDir = Environment.getExternalStorageDirectory();
//获取指定文件的输入流
FileInputStream fis = new FileInputStream(sdCardDir.getCanonicalPath() + FILE_NAME);
//将指定输入流包装成bufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringBuilder aBuilder = new StringBuilder("");
String line = null;
while((line = br.readLine()) != null){
aBuilder.append(line);
}
//关闭资源
br.close();
return aBuilder.toString();
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
/**
* 向SD卡的文件写入内容
* 使用RandomAccessFile向Sd卡指定文件追加内容
* 如果使用FileOutputStream向指定文件写入数据,
* FileOutputStream会把原有文件的内容清空,不能追缴文件
*
* */
private void write(String content){
try {
//如果手机插入了SD卡,而且应用程序具有访问SD卡的权限
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SD卡的目录
File sdCardDir = Environment.getExternalStorageDirectory();
File targetFile = new File(sdCardDir.getCanonicalPath() + FILE_NAME);
//以指定文件创建RandomAccessFile对象
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
//将文件记录指针移动到最后
raf.seek(targetFile.length());
//输出文件内容
raf.write(content.getBytes());
//关闭RandAccessFile
raf.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}