广告
返回顶部
首页 > 资讯 > 移动开发 >android 手机SD卡读写操作(以txt文本为例)实现步骤
  • 230
分享到

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

sdsd卡Android 2022-06-06 10:06:17 230人浏览 独家记忆
摘要

1、首先对manifest注册SD卡读写权限 要说明一下,我这里没有用MainActivity.class作为软件入口 代码如下: AndroidManifest.xml &

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

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;

private boolean hasSD = false;

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();
}

public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + "//" + fileName);
if (!file.exists()) {
file.createNewFile();
}
return file;
}

public boolean deleteSDFile(String fileName) {
File file = new File(SDPATH + "//" + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
return file.delete();
}

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) {
}
}

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

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开发之自动朗读TTS用法分析Android应用程序中读写txt文本文件的基本方法讲解android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法Android中实现为TextView添加多个可点击的文本Android中捕获TTextView文本中的链接点击事件方法android界面布局之实现文本块布局效果示例Android TextView多文本折叠展开效果Android UI实现多行文本折叠展开效果Android开发之文本内容自动朗读功能实现方法


--结束END--

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

本文链接: https://www.lsjlt.com/news/27886.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作