zoukankan      html  css  js  c++  java
  • sd卡文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下)

    SDPATH=Environment.getExternalStorageDirectory()+"/";

    2. 判断SD卡上的文件夹是否存在:通过File对象的exists()方法。

      /**
       * 判断文件是否已经存在;
       *
      /
      public boolean checkFileExists(String filepath) {
             File file=new File(SDPATH+filepath);
             return file.exists();
        }

    3.在SD卡上创建目录:通过File对象的mkdir()方法实现。

      /*
       * 在SD卡上创建目录;
       */
      public File createDIR(String dirpath) {
        File dir=new File(SDPATH+dirpath);
        dir.mkdir();
        return dir;
    }

    4.在SD卡上创建文件:通过File对象的createNewFile()方法实现。
      /*
       * 在SD卡上创建文件;
       */
         public File createFile(String filepath) throws IOException{
          File file=new File(SDPATH+filepath);
               file.createNewFile();
               return file;
         }

    5.将InputStream字节流写入到SD卡文件中
         /**
          * 将一个InputStream中的数据写入至SD卡中
          */
       public File writeStreamToSDCard(String dirpath,String filename,InputStream input) {
                 File file = null;
                 OutputStream output=null;
                  try {
                      //创建目录;
                      createDIR(dirpath);
                      //在创建 的目录上创建文件;
                      file = createFile(dirpath+filename);
                      output=new FileOutputStream(file);
                      byte[]bt=new byte[4*1024];
                      while (input.read(bt)!=-1) {
                         output.write(bt);
                      }
                    //刷新缓存,
                      output.flush();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  finally{

                      try{
                  output.close();
                      }
              catch (Exception e) {
                         e.printStackTrace();
                      }
                  }

                 return file;

        }


    6. 访问的权限:
    需在AndroidManifest中加上:
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    转自:http://www.cnblogs.com/onlyinweb/archive/2012/08/13/2636159.html

  • 相关阅读:
    Codeforces 1255B Fridge Lockers
    Codeforces 1255A Changing Volume
    Codeforces 1255A Changing Volume
    leetcode 112. 路径总和
    leetcode 129. 求根到叶子节点数字之和
    leetcode 404. 左叶子之和
    leetcode 104. 二叉树的最大深度
    leetcode 235. 二叉搜索树的最近公共祖先
    450. Delete Node in a BST
    树的c++实现--建立一棵树
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/3499523.html
Copyright © 2011-2022 走看看