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

android 手机SD卡读写 *** 作(以txt文本为例)实现步骤,第1张

概述1、首先对manifest注册SD卡读写权限要说明一下,我这里没有用MainActivity.class作为软件入口复制代码代码如下:AndroidManifest.xml<?xmlversion=\"1.0\"encoding=\"utf-8\"?><manifestxmlns:android=\"htt 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文本为例)实现步骤全部内容,希望文章能够帮你解决android 手机SD卡读写 *** 作(以txt文本为例)实现步骤所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1142575.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存