Android
发布时间:2022-04-28 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android 手机SD卡读写操作(以txt文本为例)实现步骤,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1、首先对manifest注册SD卡读写权限 要说明一下,我这里没有用MainActivity.class作为软件入口
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://
scheR_771_11845@a
s.android.com/apk/res/android"
package="co
m.te
s.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:allow
BACkup="true"
android:icon="@drawable/ic_launcher"
android:label="@
String/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="co
m.te
s.textsd.Fi
LeoperateActivity"
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卡中文件读写的类 File
Helper.java
/
** * @
title: File
Helper.java
* @Package co
m.te
s.textsd
* @Description: TODO(用一句话描述该
文件做什么)
* @author Alex.Z
* @date 2013-2-26 下午5
:45
:40
* @version V1.0
*/
package co
m.te
s.textsd;
import java.io.DataOutputStream;
import java.io.File;
import java.io.Fi
LeoutputStream;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Co
ntext;
import android
.os.Environment;
public class File
Helper
{ private Co
ntext co
ntext;
/
** SD卡是否存在
**/
private
Boolean hasSD =
false;
/
** SD卡的路径
**/
private
String SDPATH;
/
** 当前程序包的路径
**/
private
String FILESPATH;
public File
Helper(Co
ntext co
ntext)
{ thi
s.co
ntext = co
ntext;
hasSD = Environment.getExternalStorageState().equals(
android
.os.Environment.MEDIA_MOUNTED
); SDPATH = Environment.getExternalStorageDirectory().getPath(
); FILESPATH = thi
s.co
ntext.getFilesDir().getPath(
); }
/
** * 在SD卡上
创建文件 *
* @throws IOException
*/
public File createSDFile(
String file
Name) throws IOException
{ File file = new File(SDPATH + "//" + file
Name);
if (!file.exists())
{ file.createNewFile(
); }
return file;
}
/
** *
删除SD卡上的
文件 *
* @param fil
ename */
public
Boolean
deleteSDFile(
String file
Name) { File file = new File(SDPATH + "//" + file
Name);
if (file == null || !file.exists() || file.isDirectory())
return
false;
return file.
delete();
}
/
** * 写入
内容到SD卡中的txt文本中
* str为
内容 */
public void writeSDFile(
String str,
String file
Name) { try
{ FileWriter fw = new FileWriter(SDPATH + "//" + file
Name);
File f = new File(SDPATH + "//" + file
Name);
fw.write(str
); Fi
LeoutputStream os = new Fi
LeoutputStream(f
); DataOutputStream out = new DataOutputStream(os
); out.writeShort(2
); out.writeUTF(""
); Sy
stem.out.println(out
); fw.flush(
); fw.close(
); Sy
stem.out.println(fw
); } catch (Exception
E) { }
}
/
** * 读取SD卡
中文本
文件 *
* @param fil
ename * @return
*/
public
String readSDFile(
String file
Name) { StringBuffer sb =
new StringBuffer(
); File file = new File(SDPATH + "//" + file
Name);
try
{ FileInputStream fis = new FileInputStream(fil
E);
int
c; while ((c = fi
s.read())
!= -1)
{ sb.append((char) c
); }
fi
s.close(
); } catch (FileNotFoundException
E) { e.printStackTrace(
); } catch (IOException
E) { e.printStackTrace(
); }
return sb.to
String(
); }
public
String getFILESPATH()
{ return FILESPATH;
}
public
String getSDPATH()
{ return SDPATH;
}
public
Boolean hasSD()
{ return hasSD;
}
}
3、写一个用于检测读写功能的的布局 @H_985_2
@main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://
scheR_771_11845@a
s.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的类了 Fi
LeoperateActivity.class
/
** * @
title: Fi
LeoperateActivity.java
* @Package co
m.te
s.textsd
* @Description: TODO(用一句话描述该
文件做什么)
* @author Alex.Z
* @date 2013-2-26 下午5
:47:28
* @version V1.0
*/
package co
m.te
s.textsd;
import java.io.IOException;
import android.app.Activity;
import android
.os
.bundle;
import android.widget.TextView;
public class Fi
LeoperateActivity extends Activity
{ private TextView hasSDTextView;
private TextView SDPathTextView;
private TextView FILESpathTextView;
private TextView createFileTextView;
private TextView readFileTextView;
private TextView
deleteFileTextView;
private File
Helper
Helper;
@Override
public void onCreate(Bundle savedInstanceStat
E) { super
.onCreate(savedInstanceStat
E);
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 File
Helper(getApplicationCo
ntext()
); 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文本为例)实现步骤所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。