欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android 手機(jī)SD卡讀寫(xiě)操作(以txt文本為例)實(shí)現(xiàn)步驟

 更新時(shí)間:2013年02月27日 10:28:37   作者:  
要完成SD卡讀寫(xiě)操作首先對(duì)manifest注冊(cè)SD卡讀寫(xiě)權(quán)限其次是創(chuàng)建一個(gè)對(duì)SD卡中文件讀寫(xiě)的類(lèi)寫(xiě)一個(gè)用于檢測(cè)讀寫(xiě)功能的的布局然后就是UI的類(lèi)了,感興趣的朋友可以參考下,希望可以幫助到你
1、首先對(duì)manifest注冊(cè)SD卡讀寫(xiě)權(quán)限
要說(shuō)明一下,我這里沒(méi)有用MainActivity.class作為軟件入口
復(fù)制代碼 代碼如下:

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、創(chuàng)建一個(gè)對(duì)SD卡中文件讀寫(xiě)的類(lèi)
復(fù)制代碼 代碼如下:

FileHelper.java
/**
* @Title: FileHelper.java
* @Package com.tes.textsd
* @Description: TODO(用一句話(huà)描述該文件做什么)
* @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;
/** 當(dāng)前程序包的路徑**/
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卡上創(chuàng)建文件
*
* @throws IOException
*/
public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + "http://" + fileName);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
/**
* 刪除SD卡上的文件
*
* @param fileName
*/
public boolean deleteSDFile(String fileName) {
File file = new File(SDPATH + "http://" + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
return file.delete();
}
/**
* 寫(xiě)入內(nèi)容到SD卡中的txt文本中
* str為內(nèi)容
*/
public void writeSDFile(String str,String fileName)
{
try {
FileWriter fw = new FileWriter(SDPATH + "http://" + fileName);
File f = new File(SDPATH + "http://" + 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 + "http://" + 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、寫(xiě)一個(gè)用于檢測(cè)讀寫(xiě)功能的的布局
復(fù)制代碼 代碼如下:

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的類(lèi)了
復(fù)制代碼 代碼如下:

FileOperateActivity.class
/**
* @Title: FileOperateActivity.java
* @Package com.tes.textsd
* @Description: TODO(用一句話(huà)描述該文件做什么)
* @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("創(chuàng)建文件:"
+ 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"));
}
}

看看運(yùn)行的效果:

相關(guān)文章

  • Android Flutter實(shí)現(xiàn)點(diǎn)贊效果的示例代碼

    Android Flutter實(shí)現(xiàn)點(diǎn)贊效果的示例代碼

    點(diǎn)贊這個(gè)動(dòng)作不得不說(shuō)在社交、短視頻等App中實(shí)在是太常見(jiàn)了。本文將利用Flutter制作出一個(gè)點(diǎn)贊動(dòng)畫(huà)效果,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-04-04
  • 淺談Android硬件加速原理與實(shí)現(xiàn)簡(jiǎn)介

    淺談Android硬件加速原理與實(shí)現(xiàn)簡(jiǎn)介

    這篇文章主要介紹了淺談Android硬件加速原理與實(shí)現(xiàn)簡(jiǎn)介,本文嘗試從底層硬件原理,一直到上層代碼實(shí)現(xiàn),對(duì)硬件加速技術(shù)進(jìn)行簡(jiǎn)單介紹,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android Toast提示封裝實(shí)例代碼

    Android Toast提示封裝實(shí)例代碼

    這篇文章主要介紹了Android Toast提示封裝實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android數(shù)據(jù)庫(kù)greenDAO配置與使用介紹

    Android數(shù)據(jù)庫(kù)greenDAO配置與使用介紹

    這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫(kù),使用數(shù)據(jù)庫(kù)存儲(chǔ)時(shí)候,一般都會(huì)使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下
    2023-03-03
  • Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄

    Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android原生嵌入React Native詳解

    Android原生嵌入React Native詳解

    這篇文章主要為大家詳細(xì)介紹了Android原生嵌入React Native的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件

    android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件

    本篇文章主要介紹了android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件,有興趣的同學(xué)可以了解一下。
    2016-11-11
  • Android多媒體教程之播放視頻的四種方法

    Android多媒體教程之播放視頻的四種方法

    這篇文章主要給大家介紹了關(guān)于Android多媒體教程之播放視頻的四種方法,分別是通過(guò)intent的方式,調(diào)用系統(tǒng)自帶的播放器、使用VideoView、MediaPlayer + SurfaceView及MediaPlayer + TextureView等方法,需要的朋友們可以參考學(xué)習(xí)。
    2017-06-06
  • Android 日志系統(tǒng)Logger源代碼詳細(xì)介紹

    Android 日志系統(tǒng)Logger源代碼詳細(xì)介紹

    本文主要介紹Android 日志系統(tǒng)Logger,這里整理了關(guān)于Android源碼的日志系統(tǒng)資料,有研究Android源碼的朋友可以參考下
    2016-08-08
  • Android中使用Alarm的方法小結(jié)

    Android中使用Alarm的方法小結(jié)

    Alarm是android提供的用于完成鬧鐘式定時(shí)任務(wù)的類(lèi),系統(tǒng)通過(guò)AlarmManager來(lái)管理所有的Alarm,下面這篇文章主要給大家介紹了關(guān)于Android中使用Alarm的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05

最新評(píng)論