Android的SD卡文件读写
来源:互联网 发布:一千零一夜 淘宝 视频 编辑:程序博客网 时间:2024/05/29 03:13
使用右上角的两个按钮可以将文件从模拟器中导出和导入
程序运行的结果
运行之后,文件浏览器中的delete被删除了。
FileHelper.java是文件的帮助类,完成文件创建、删除、读。
package com.zeph.android.fileoperate;import java.io.File;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卡中文本文件 * * @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;}}
Activity类
package com.zeph.android.fileoperate;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;@Overridepublic 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("delete.txt"));readFileTextView.setText("读取文件:" + helper.readSDFile("benzeph.txt"));}}
Layout.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="@string/hello" /> <TextView android:id="@+id/SDPathTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/FILESpathTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/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>
注意一定要在Manifest中加入读取外部设备的条件允许,我之前就是一直忘记加入,导致文件老是不能创建和删除。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- Android的SD卡文件读写
- Android的SD卡文件读写
- Android 读写SD卡的文件
- android读写SD卡上的文件
- Android的SD卡文件读写操作
- android 读写SD卡文件
- Android 读写SD卡文件
- 读写sd卡的文件
- Android-SD卡的读写
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Android SD卡简单的文件读写操作
- Myxls组件导入导出Excel(不需安装Excel)
- mini2440 qt4 移植
- ACE_Message_Block功能简介
- 使用SAX解析XML利用Google Weather服务制作天气预报异常解决办法
- 先进的仓库管理模式
- Android的SD卡文件读写
- PHP发送邮箱类及应用
- C#生成Excel报表 用MyXls组件生成更完美
- 包的设计原则
- EF Code-First 自定义表映射
- Entity Framework 4.1 之一 : 基础
- ServletConfig和ServletContext
- Android TextView中文字设置超链接、颜色、字体
- Linux C 消息队列和共享内存