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

Android編程之在SD卡上進(jìn)行文件讀寫(xiě)操作實(shí)例詳解

 更新時(shí)間:2015年12月16日 10:54:32   作者:Sunnyfans  
這篇文章主要介紹了Android編程之在SD卡上進(jìn)行文件讀寫(xiě)操作的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android的文件操作及針對(duì)SD卡的存取操作相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程之在SD卡上進(jìn)行文件讀寫(xiě)操作的方法。分享給大家供大家參考,具體如下:

很多知識(shí)只有真正理解掌握之后才能運(yùn)用自如,舉一反三。對(duì)Java中的文件操作和android系統(tǒng)SD卡里面的文件操作,你覺(jué)得有區(qū)別嗎,顯然沒(méi)有本質(zhì)區(qū)別,如果勉強(qiáng)說(shuō)有,那也是不足為道滴,但我們?cè)趯?shí)際運(yùn)用中卻要注意如下幾點(diǎn),不然問(wèn)題會(huì)纏上你。

1、首先想要對(duì)android系統(tǒng)SD卡里文件操作需要添加使用權(quán)限:

android系統(tǒng)是不會(huì)讓外來(lái)程序隨意動(dòng)自己內(nèi)存的,如果沒(méi)有許可證,不好意思,不準(zhǔn)你動(dòng)我地盤(pán),在我地盤(pán)得聽(tīng)我的。在配置文件里AndroidManifest.xml里面添加SD卡讀寫(xiě)數(shù)據(jù)權(quán)限。

復(fù)制代碼 代碼如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2、獲取文件絕對(duì)路徑:

這里有兩種方法

方法1:直接使用/mnt/sdcard/+文件名
如: final String FILE_NAME = "/mnt/sdcard/00000.H264";

方法2:通過(guò)系統(tǒng)提供的方法獲取SD的路徑,然后在后面加上文件名稱。這個(gè)方法有點(diǎn)啰嗦,暫沒(méi)感受到它的妙處,不過(guò)在此還是羅列出來(lái),供以后參考。見(jiàn)后面實(shí)例。

3、獲取文件路徑后的操作,跳不出對(duì)FileInputStream、FileOutputStream、FileReader、FileWriter四個(gè)類的使用。具體參看前面《Java編程之文件讀寫(xiě)

下附將數(shù)據(jù)寫(xiě)入SD卡文件的簡(jiǎn)單運(yùn)用代碼:

方法1運(yùn)用代碼:

public class FileTestActivity extends Activity
{
final String FILE_NAME = "/mnt/sdcard/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
try
{
FileOutputStream fout = new FileOutputStream(FILE_NAME,
true);
BufferedOutputStream bout = new BufferedOutputStream(fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}

方法2運(yùn)用實(shí)例代碼

public class FileTestActivity extends Activity
{
final String FILE_NAME = "/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
File sdDir = Environment.getExternalStorageDirectory();
System.out.println(sdDir);
FileOutputStream fout;
try
{
System.out.println(sdDir.getCanonicalPath() + FILE_NAME);
fout = new FileOutputStream(sdDir.getCanonicalPath()
+ FILE_NAME, true);
BufferedOutputStream bout = new BufferedOutputStream(
fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
}

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論