android SD卡读写操作(以txt文本为例)
1, 首先对manifest注册SD卡读写权限
要说明一下,我这里没有用MainActivity.class作为软件入口
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.tes.textsd" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="16" /> 10 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.tes.textsd.FileOperateActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>
2, 创建一个对SD卡中文件读写的类
FileHelper.java
1 /** 2 * @Title: FileHelper.java 3 * @Package com.tes.textsd 4 * @Description: TODO(用一句话描述该文件做什么) 5 * @author Alex.Z 6 * @date 2013-2-26 下午5:45:40 7 * @version V1.0 8 */ 9 package com.tes.textsd; 10 11 import java.io.DataOutputStream; 12 import java.io.File; 13 import java.io.FileOutputStream; 14 import java.io.FileWriter; 15 16 import java.io.FileInputStream; 17 18 import java.io.FileNotFoundException; 19 20 import java.io.IOException; 21 22 23 24 import android.content.Context; 25 26 import android.os.Environment; 27 28 29 30 public class FileHelper { 31 32 33 34 private Context context; 35 36 /** SD卡是否存在**/ 37 38 private boolean hasSD = false; 39 40 /** SD卡的路径**/ 41 42 private String SDPATH; 43 44 /** 当前程序包的路径**/ 45 46 private String FILESPATH; 47 48 49 50 public FileHelper(Context context) { 51 52 this.context = context; 53 54 hasSD = Environment.getExternalStorageState().equals( 55 56 android.os.Environment.MEDIA_MOUNTED); 57 58 SDPATH = Environment.getExternalStorageDirectory().getPath(); 59 60 FILESPATH = this.context.getFilesDir().getPath(); 61 62 } 63 64 65 66 /** 67 68 * 在SD卡上创建文件 69 70 * 71 72 * @throws IOException 73 74 */ 75 76 public File createSDFile(String fileName) throws IOException { 77 78 File file = new File(SDPATH + "//" + fileName); 79 80 if (!file.exists()) { 81 82 file.createNewFile(); 83 84 } 85 86 return file; 87 88 } 89 90 91 92 /** 93 94 * 删除SD卡上的文件 95 96 * 97 98 * @param fileName 99 100 */ 101 102 public boolean deleteSDFile(String fileName) { 103 104 File file = new File(SDPATH + "//" + fileName); 105 106 if (file == null || !file.exists() || file.isDirectory()) 107 108 return false; 109 110 return file.delete(); 111 112 } 113 /** 114 * 写入内容到SD卡中的txt文本中 115 * str为内容 116 */ 117 118 public void writeSDFile(String str,String fileName) 119 { 120 try { 121 FileWriter fw = new FileWriter(SDPATH + "//" + fileName); 122 File f = new File(SDPATH + "//" + fileName); 123 fw.write(str); 124 125 FileOutputStream os = new FileOutputStream(f); 126 DataOutputStream out = new DataOutputStream(os); 127 out.writeShort(2); 128 out.writeUTF(""); 129 System.out.println(out); 130 fw.flush(); 131 fw.close(); 132 System.out.println(fw); 133 } catch (Exception e) { 134 } 135 } 136 137 /** 138 139 * 读取SD卡中文本文件 140 141 * 142 143 * @param fileName 144 145 * @return 146 147 */ 148 149 public String readSDFile(String fileName) { 150 151 StringBuffer sb = new StringBuffer(); 152 153 File file = new File(SDPATH + "//" + fileName); 154 155 try { 156 157 FileInputStream fis = new FileInputStream(file); 158 159 int c; 160 161 while ((c = fis.read()) != -1) { 162 163 sb.append((char) c); 164 165 } 166 167 fis.close(); 168 169 } catch (FileNotFoundException e) { 170 171 e.printStackTrace(); 172 173 } catch (IOException e) { 174 175 e.printStackTrace(); 176 177 } 178 179 return sb.toString(); 180 181 } 182 183 184 185 public String getFILESPATH() { 186 187 return FILESPATH; 188 189 } 190 191 192 193 public String getSDPATH() { 194 195 return SDPATH; 196 197 } 198 199 200 201 public boolean hasSD() { 202 203 return hasSD; 204 205 } 206 207 }
3, 写一个用于检测读写功能的的布局
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:layout_width="fill_parent" 6 7 android:layout_height="fill_parent" 8 9 android:orientation="vertical" > 10 11 12 13 <TextView 14 15 android:id="@+id/hasSDTextView" 16 17 android:layout_width="fill_parent" 18 19 android:layout_height="wrap_content" 20 21 android:text="hello" /> 22 23 24 25 <TextView 26 27 android:id="@+id/SDPathTextView" 28 29 android:layout_width="fill_parent" 30 31 android:layout_height="wrap_content" 32 33 android:text="hello" /> 34 35 36 37 <TextView 38 39 android:id="@+id/FILESpathTextView" 40 41 android:layout_width="fill_parent" 42 43 android:layout_height="wrap_content" 44 45 android:text="hello" /> 46 47 48 49 <TextView 50 51 android:id="@+id/createFileTextView" 52 53 android:layout_width="fill_parent" 54 55 android:layout_height="wrap_content" 56 57 android:text="false" /> 58 59 60 61 <TextView 62 63 android:id="@+id/readFileTextView" 64 65 android:layout_width="fill_parent" 66 67 android:layout_height="wrap_content" 68 69 android:text="false" /> 70 71 72 73 <TextView 74 75 android:id="@+id/deleteFileTextView" 76 77 android:layout_width="fill_parent" 78 79 android:layout_height="wrap_content" 80 81 android:text="false" /> 82 83 84 85 </LinearLayout>
4, 就是UI的类了
FileOperateActivity.class
1 /** 2 * @Title: FileOperateActivity.java 3 * @Package com.tes.textsd 4 * @Description: TODO(用一句话描述该文件做什么) 5 * @author Alex.Z 6 * @date 2013-2-26 下午5:47:28 7 * @version V1.0 8 */ 9 package com.tes.textsd; 10 11 import java.io.IOException; 12 13 14 15 import android.app.Activity; 16 17 import android.os.Bundle; 18 19 import android.widget.TextView; 20 21 22 23 public class FileOperateActivity extends Activity { 24 25 26 27 private TextView hasSDTextView; 28 29 private TextView SDPathTextView; 30 31 private TextView FILESpathTextView; 32 33 private TextView createFileTextView; 34 35 private TextView readFileTextView; 36 37 private TextView deleteFileTextView; 38 39 private FileHelper helper; 40 41 42 43 @Override 44 45 public void onCreate(Bundle savedInstanceState) { 46 47 super.onCreate(savedInstanceState); 48 49 setContentView(R.layout.main); 50 51 52 53 hasSDTextView = (TextView) findViewById(R.id.hasSDTextView); 54 55 SDPathTextView = (TextView) findViewById(R.id.SDPathTextView); 56 57 FILESpathTextView = (TextView) findViewById(R.id.FILESpathTextView); 58 59 createFileTextView = (TextView) findViewById(R.id.createFileTextView); 60 61 readFileTextView = (TextView) findViewById(R.id.readFileTextView); 62 63 deleteFileTextView = (TextView) findViewById(R.id.deleteFileTextView); 64 65 66 67 helper = new FileHelper(getApplicationContext()); 68 69 70 71 hasSDTextView.setText("SD卡是否存在:" + helper.hasSD()); 72 73 SDPathTextView.setText("SD卡路径:" + helper.getSDPATH()); 74 75 FILESpathTextView.setText("包路径:" + helper.getFILESPATH()); 76 77 78 79 try { 80 81 createFileTextView.setText("创建文件:" 82 83 + helper.createSDFile("test.txt").getAbsolutePath()); 84 85 } catch (IOException e) { 86 87 e.printStackTrace(); 88 89 } 90 91 92 93 deleteFileTextView.setText("删除文件是否成功:" 94 95 + helper.deleteSDFile("xx.txt")); 96 97 helper.writeSDFile("1213212", "test.txt"); 98 99 readFileTextView.setText("读取文件:" + helper.readSDFile("test.txt")); 100 101 } 102 103 }
看看运行的效果:


浙公网安备 33010602011771号