-
首先对manifest注册SD卡读写权限
1234567891011121314151617181920212223242526AndroidManifest.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns: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>
-
创建一个对SD卡中文件读写的类
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111FileHelper.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;
}
}
-
helper =
new
FileHelper(getApplicationContext());
创建文件:"
+helper.createSDFile(
"test.txt"
).getAbsolutePath());
"删除文件是否成功:"
helper.deleteSDFile(
"xx.txt"
));
helper.writeSDFile(
"1213212"
,
"test.txt"
);
"读取文件:"
+ helper.readSDFile(
"test.txt"
));