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();

發佈了13 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章