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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值