Android对sdcard扩展卡文件操作实例详解

2019-12-10 20:01:14王旭
  • */  private void onWrite() { 
  • try {  Log.d(LOG_TAG, "Start Write"); 
  • //1.判断是否存在sdcard  if (Environment.MEDIA_MOUNTED.equals(Environment 
  • .getExternalStorageState())) {  //目录 
  • File path = new File(PATH);  //文件 
  • File f = new File(PATH + FILENAME);  if(!path.exists()){ 
  • //2.创建目录,可以在应用启动的时候创建  path.mkdirs(); 
  • }  if (!f.exists()) { 
  • //3.创建文件  f.createNewFile(); 
  • }  OutputStreamWriter osw = new OutputStreamWriter( 
  • new FileOutputStream(f));  //4.写文件,从EditView获得文本值 
  • osw.write(editor.getText().toString());  osw.close(); 
  • }  } catch (Exception e) { 
  • Log.d(LOG_TAG, "file create error");  }