当前位置: 编程技术>移动开发
android 手机SD卡读写操作(以txt文本为例)实现步骤
来源: 互联网 发布时间:2014-10-13
本文导语: 1、首先对manifest注册SD卡读写权限 要说明一下,我这里没有用MainActivity.class作为软件入口 代码如下: AndroidManifest.xml 2、创建一个对SD卡中文件读写的类 代码如下: FileHelper.java /** * @Title: FileHelper.java * @Package com.tes.texts...
1、首先对manifest注册SD卡读写权限
要说明一下,我这里没有用MainActivity.class作为软件入口
AndroidManifest.xml
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
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"));
}
}
看看运行的效果:
要说明一下,我这里没有用MainActivity.class作为软件入口
代码如下:
AndroidManifest.xml
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
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"));
}
}
看看运行的效果: