android 手机SD卡读写操作(以txt文本为例)实现步骤

时间:2022-01-07 00:40:24

1、首先对manifest注册sd卡读写权限
要说明一下,我这里没有用mainactivity.class作为软件入口

复制代码 代码如下:

androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tes.textsd"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="8"
android:targetsdkversion="16" />
<uses-permission android:name="android.permission.write_external_storage"/>
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<activity
android:name="com.tes.textsd.fileoperateactivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>


2、创建一个对sd卡中文件读写的类

复制代码 代码如下:

filehelper.java
/**
* @title: filehelper.java
* @package com.tes.textsd
* @description: todo(用一句话描述该文件做什么)
* @author alex.z
* @date 2013-2-26 下午5:45:40
* @version v1.0
*/
package com.tes.textsd;
import java.io.dataoutputstream;
import java.io.file;
import java.io.fileoutputstream;
import java.io.filewriter;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import android.content.context;
import android.os.environment;
public class filehelper {
private context context;
/** sd卡是否存在**/
private boolean hassd = false;
/** sd卡的路径**/
private string sdpath;
/** 当前程序包的路径**/
private string filespath;
public filehelper(context context) {
this.context = context;
hassd = environment.getexternalstoragestate().equals(
android.os.environment.media_mounted);
sdpath = environment.getexternalstoragedirectory().getpath();
filespath = this.context.getfilesdir().getpath();
}
/**
* 在sd卡上创建文件
*
* @throws ioexception
*/
public file createsdfile(string filename) throws ioexception {
file file = new file(sdpath + "//" + filename);
if (!file.exists()) {
file.createnewfile();
}
return file;
}
/**
* 删除sd卡上的文件
*
* @param filename
*/
public boolean deletesdfile(string filename) {
file file = new file(sdpath + "//" + filename);
if (file == null || !file.exists() || file.isdirectory())
return false;
return file.delete();
}
/**
* 写入内容到sd卡中的txt文本中
* str为内容
*/
public void writesdfile(string str,string filename)
{
try {
filewriter fw = new filewriter(sdpath + "//" + filename);
file f = new file(sdpath + "//" + filename);
fw.write(str);
fileoutputstream os = new fileoutputstream(f);
dataoutputstream out = new dataoutputstream(os);
out.writeshort(2);
out.writeutf("");
system.out.println(out);
fw.flush();
fw.close();
system.out.println(fw);
} catch (exception e) {
}
}
/**
* 读取sd卡中文本文件
*
* @param filename
* @return
*/
public string readsdfile(string filename) {
stringbuffer sb = new stringbuffer();
file file = new file(sdpath + "//" + filename);
try {
fileinputstream fis = new fileinputstream(file);
int c;
while ((c = fis.read()) != -1) {
sb.append((char) c);
}
fis.close();
} catch (filenotfoundexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return sb.tostring();
}
public string getfilespath() {
return filespath;
}
public string getsdpath() {
return sdpath;
}
public boolean hassd() {
return hassd;
}
}


3、写一个用于检测读写功能的的布局

复制代码 代码如下:

main.xml
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<textview
android:id="@+id/hassdtextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<textview
android:id="@+id/sdpathtextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<textview
android:id="@+id/filespathtextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<textview
android:id="@+id/createfiletextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="false" />
<textview
android:id="@+id/readfiletextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="false" />
<textview
android:id="@+id/deletefiletextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="false" />
</linearlayout>


4、就是ui的类了

复制代码 代码如下:

fileoperateactivity.class
/**
* @title: fileoperateactivity.java
* @package com.tes.textsd
* @description: todo(用一句话描述该文件做什么)
* @author alex.z
* @date 2013-2-26 下午5:47:28
* @version v1.0
*/
package com.tes.textsd;
import java.io.ioexception;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class fileoperateactivity extends activity {
private textview hassdtextview;
private textview sdpathtextview;
private textview filespathtextview;
private textview createfiletextview;
private textview readfiletextview;
private textview deletefiletextview;
private filehelper helper;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
hassdtextview = (textview) findviewbyid(r.id.hassdtextview);
sdpathtextview = (textview) findviewbyid(r.id.sdpathtextview);
filespathtextview = (textview) findviewbyid(r.id.filespathtextview);
createfiletextview = (textview) findviewbyid(r.id.createfiletextview);
readfiletextview = (textview) findviewbyid(r.id.readfiletextview);
deletefiletextview = (textview) findviewbyid(r.id.deletefiletextview);
helper = new filehelper(getapplicationcontext());
hassdtextview.settext("sd卡是否存在:" + helper.hassd());
sdpathtextview.settext("sd卡路径:" + helper.getsdpath());
filespathtextview.settext("包路径:" + helper.getfilespath());
try {
createfiletextview.settext("创建文件:"
+ helper.createsdfile("test.txt").getabsolutepath());
} catch (ioexception e) {
e.printstacktrace();
}
deletefiletextview.settext("删除文件是否成功:"
+ helper.deletesdfile("xx.txt"));
helper.writesdfile("1213212", "test.txt");
readfiletextview.settext("读取文件:" + helper.readsdfile("test.txt"));
}
}


看看运行的效果:
android 手机SD卡读写操作(以txt文本为例)实现步骤