日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Android 读写SDcard (转)

發布時間:2023/12/29 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 Android 读写SDcard (转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近有個想法,就是想實現一個讀寫文件實例,一開始只想讀寫簡單的文件(如txt格式文件),后面想到了讀寫XML文件,其實無論是寫txt文件還是XML文件,其實寫入的值都是一個字符串值,所以關鍵是如何實現讀寫sdcard里面的文件。

下面我講一下如何如寫和讀txt文件,其實XML文件同樣,只不過在寫和讀之前還要做相應的處理而已

如果你要寫SDcard文件,首先你就要有寫sdcard操作權限。即要在AndroidManifest.xml文件中加入申請權限


<!-- 讀寫SDK卡一定要申請權限允可 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然后你就可以進行寫和讀操作了。


/**
* 寫入指定XML文件中的內容
* @param file 文件
* @param destDirStr 文件目錄
* @param szOutText 文件內容
* @return
*/
public String writeToSDcardFile(String file, String destDirStr,
String szOutText) {
// 獲取擴展SD卡設備狀態
String sDStateString = android.os.Environment.getExternalStorageState();

File myFile = null;
// 擁有可讀可寫權限
if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {

try {

// 獲取擴展存儲設備的文件目錄
File SDFile = android.os.Environment
.getExternalStorageDirectory();

// File destDir=new File("/sdcard/xmlfile");

File destDir = new File(SDFile.getAbsolutePath() + destDirStr);

if (!destDir.exists())
destDir.mkdir();
// Toast.makeText(SDCardTest., text, duration)
// 打開文件
myFile = new File(destDir + File.separator + file);

// 判斷是否存在,不存在則創建
if (!myFile.exists()) {
myFile.createNewFile();
}

// 寫數據
// String szOutText = "Hello, World!";
FileOutputStream outputStream = new FileOutputStream(myFile);
outputStream.write(szOutText.getBytes());
outputStream.close();

} catch (Exception e) {
// TODO: handle exception
}// end of try

}// end of if(MEDIA_MOUNTED)
// 擁有只讀權限
else if (sDStateString
.endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {

// 獲取擴展存儲設備的文件目錄
File SDFile = android.os.Environment.getExternalStorageDirectory();

// 創建一個文件
myFile = new File(SDFile.getAbsolutePath() + destDirStr
+ File.separator + file);

// 判斷文件是否存在,存在的情況下才去讀該文件
if (myFile.exists()) {
try {

// 讀數據
FileInputStream inputStream = new FileInputStream(myFile);
byte[] buffer = new byte[1024];
inputStream.read(buffer);
inputStream.close();

} catch (Exception e) {
// TODO: handle exception
}// end of try
}// end of if(myFile)
}// end of if(MEDIA_MOUNTED_READ_ONLY)
// end of func

return myFile.toString();
}

/**
* 讀出指定XML文件中的內容
* @param file 文件
* @param destDirStr 文件目錄
* @param szOutText 文件內容
* @return 返回文件內容
*/
public String readContentFromFile(String file, String destDirStr){

String content=null;
// 獲取擴展存儲設備的文件目錄
File SDFile = android.os.Environment.getExternalStorageDirectory();

// 創建一個文件
File myFile = new File(SDFile.getAbsolutePath() + destDirStr
+ File.separator + file);

// 判斷文件是否存在,存在的情況下才去讀該文件
if (myFile.exists()) {
try {

// 讀數據
FileInputStream inputStream = new FileInputStream(myFile);
byte[] buffer = new byte[1024];
inputStream.read(buffer);
inputStream.close();

content=new String(buffer);

} catch (Exception e) {
// TODO: handle exception
}// end of try
}// end of if(myFile)

return content;
}

然后我們在外部類實現調用

寫入指定文件:

SDCardOperate sdOperate=new SDCardOperate();
sdOperate.writeToSDcardFile("test.xml","/xmlfiles/","helloworld" );

//XMLWriterOperate.writeXml()

從指定文件中讀出:

SDCardOperate sdOperate=new SDCardOperate();
String content=sdOperate.readContentFromFile("test.xml","/xmlfiles/");

Toast.makeText(SQL2.this, content, Toast.LENGTH_LONG).show();

實現效果如下所示:

用Toast顯示出效果圖“:

總結

以上是生活随笔為你收集整理的Android 读写SDcard (转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。