android 手機SD卡讀寫操作(以txt文本為例)實現步驟

來源:互聯網
上載者:User

1、首先對manifest註冊SD卡讀寫權限
要說明一下,我這裡沒有用MainActivity.class作為軟體入口 複製代碼 代碼如下:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/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>

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
<?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="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的類了 複製代碼 代碼如下: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"));
}
}

看看啟動並執行效果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.