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

 更新时间:2013年02月27日 10:28:37   作者:  
要完成SD卡读写操作首先对manifest注册SD卡读写权限其次是创建一个对SD卡中文件读写的类写一个用于检测读写功能的的布局然后就是UI的类了,感兴趣的朋友可以参考下,希望可以帮助到你
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实现侧滑菜单DrawerLayout

    Android实现侧滑菜单DrawerLayout

    这篇文章主要为大家详细介绍了Android实现侧滑菜单DrawerLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • AndriodStudio使用listview实现简单图书管理

    AndriodStudio使用listview实现简单图书管理

    这篇文章主要为大家详细介绍了AndriodStudio使用listview实现简单图书管理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • ffmpeg实现去水印以及切分视频demo

    ffmpeg实现去水印以及切分视频demo

    这篇文章主要为大家介绍了ffmpeg实现去水印以及切分视频demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Android实现水平带刻度的进度条

    Android实现水平带刻度的进度条

    这篇文章主要为大家详细介绍了Android实现水平带刻度的进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android沉浸式状态栏实现示例

    Android沉浸式状态栏实现示例

    本篇文章主要介绍了Android沉浸式状态栏实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • android图库竖屏不显示status bar的解决方法

    android图库竖屏不显示status bar的解决方法

    图库在JB和JB2的版本上显示的行为是:横屏全屏显示,竖屏会显示status bar,图库在JB和JB2的版本上显示的行为是:横屏全屏显示,竖屏会显示status bar,具体实现方法如下,不会的朋友可以参考下哈
    2013-06-06
  • Android中ScrollView嵌套GridView的解决办法

    Android中ScrollView嵌套GridView的解决办法

    前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全。下面小编给大家分享下解决方案,需要的朋友可以参考下
    2017-04-04
  • Android开发之对话框案例详解(五种对话框)

    Android开发之对话框案例详解(五种对话框)

    本文通过实例代码给大家分享了5种android对话框,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09
  • Android仿支付宝支付密码输入框

    Android仿支付宝支付密码输入框

    这篇文章主要为大家详细介绍了Android仿支付宝支付密码输入框的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android从触碰屏幕开始的事件采集解析及分发

    Android从触碰屏幕开始的事件采集解析及分发

    这篇文章主要为大家介绍了Android从触碰屏幕开始的事件采集解析及分发,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06

最新评论