生活随笔
收集整理的這篇文章主要介紹了
getCacheDir用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注: 在Activity中有 getFileDir() 和 getCacheDir(); 方法可以獲得當前的手機自帶的存儲空間中的當前包文件的路徑
getFileDir() ----- /data/data/cn.xxx.xxx(當前包)/files
getCacheDir() ----- /data/data/cn.xxx.xxx(當前包)/cache
??1.?編寫文件讀取與寫入功能實現類?FileService ????????package?cn.android.service; ????????import?java.io.ByteArrayOutputStream; ??????import?java.io.FileInputStream; ??????import?java.io.FileOutputStream; ????????import?android.content.Context; ??????import?android.util.Log; ???????????????????public?class?FileService?{ ????????????public?static?final?String?TAG?=?"FileService"; ??????????private?Context?context; ??????????????????????public?FileService(Context?context)?{ ??????????????this.context?=?context; ??????????} ????????????????????????????public?void?save(String?fileName,?String?content)?throws?Exception?{ ??????????????????????????????if?(!fileName.endsWith(".txt"))?{ ??????????????????fileName?=?fileName?+?".txt"; ??????????????} ?????????????? ??????????????byte[]?buf?=?fileName.getBytes("iso8859-1"); ?????????????? ??????????????Log.e(TAG,?new?String(buf,"utf-8")); ?????????????? ??????????????fileName?=?new?String(buf,"utf-8"); ?????????????? ??????????????Log.e(TAG,?fileName); ?????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????FileOutputStream?fos?=?context.openFileOutput(fileName,?context.MODE_PRIVATE); ??????????????fos.write(content.getBytes()); ??????????????fos.close(); ??????????} ????????????????????????????public?String?read(String?fileName)?throws?Exception?{ ??????????????????????????????if?(!fileName.endsWith(".txt"))?{ ??????????????????fileName?=?fileName?+?".txt"; ??????????????} ????????????????FileInputStream?fis?=?context.openFileInput(fileName); ??????????????ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream(); ????????????????byte[]?buf?=?new?byte[1024]; ??????????????int?len?=?0; ??????????????????????????????while?((len?=?fis.read(buf))?!=?-1)?{ ??????????????????baos.write(buf,?0,?len); ??????????????} ????????????????fis.close(); ??????????????baos.close(); ??????????????????????????????return?baos.toString(); ????????????} ????????} ????2.?編寫Activity類: ??????package?cn.android.test; ????????import?android.app.Activity; ??????import?android.os.Bundle; ??????import?android.util.Log; ??????import?android.view.View; ??????import?android.widget.Button; ??????import?android.widget.EditText; ??????import?android.widget.Toast; ??????import?cn.android.service.FileService; ????????public?class?TestAndroidActivity?extends?Activity?{ ???????????????????? ????????????????????private?FileService?fileService?=?new?FileService(this); ????????????????????private?EditText?fileNameText; ????????????????????private?EditText?contentText; ????????????????????private?Toast?toast; ???????????? ??????????@Override??????????public?void?onCreate(Bundle?savedInstanceState)?{ ??????????super.onCreate(savedInstanceState); ??????????setContentView(R.layout.main); ???????????? ????????????????????Button?button?=?(Button)this.findViewById(R.id.button); ??????????Button?read?=?(Button)this.findViewById(R.id.read); ??????????fileNameText?=?(EditText)?this.findViewById(R.id.filename); ??????????contentText?=?(EditText)?this.findViewById(R.id.content); ?????????? ????????????????????button.setOnClickListener(new?View.OnClickListener()?{ ??????????????????@Override??????????????????public?void?onClick(View?v)?{ ?????????????????????? ??????????????????????String?fileName?=?fileNameText.getText().toString(); ??????????????????????String?content?=?contentText.getText().toString(); ?????????????????????? ????????????????????????????????????????????if(isEmpty(fileName))?{ ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.empty_filename,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.w(fileService.TAG,?"The?file?name?is?empty"); ??????????????????????????return; ??????????????????????} ?????????????????????? ????????????????????????????????????????????if(isEmpty(content))?{ ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.empty_content,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.w(fileService.TAG,?"The?file?content?is?empty"); ??????????????????????????return; ??????????????????????} ?????????????????????? ????????????????????????????????????????????????????????????????????????????????????????try?{ ??????????????????????????fileService.save(fileName,?content); ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.success,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.i(fileService.TAG,?"The?file?save?successful"); ??????????????????????}?catch?(Exception?e)?{ ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.fail,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.e(fileService.TAG,?"The?file?save?failed"); ??????????????????????} ?????????????????????? ??????????????????} ??????????}); ?????????? ?????????? ????????????????????read.setOnClickListener(new?View.OnClickListener()?{ ??????????????????@Override??????????????????public?void?onClick(View?v)?{ ?????????????????????? ????????????????????????????????????????????String?fileName?=?fileNameText.getText().toString(); ?????????????????????? ????????????????????????????????????????????if(isEmpty(fileName))?{ ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.empty_filename,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.w(fileService.TAG,?"The?file?name?is?empty"); ??????????????????????????return; ??????????????????????} ?????????????????????? ????????????????????????????????????????????????????????????????????????????????????????try?{ ??????????????????????????contentText.setText(fileService.read(fileName)); ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.read_success,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.i(fileService.TAG,?"The?file?read?successful"); ??????????????????????}?catch?(Exception?e)?{ ??????????????????????????toast?=?Toast.makeText(TestAndroidActivity.this,?R.string.read_fail,?Toast.LENGTH_LONG); ??????????????????????????toast.setMargin(RESULT_CANCELED,?0.345f);??????????????????????????????????? ??????????????????????????toast.show();??? ??????????????????????????Log.e(fileService.TAG,?"The?file?read?failed"); ??????????????????????} ??????????????????} ??????????}); ?????????? ?????????? ??????????} ?????????? ????????????????????private?boolean?isEmpty(String?s)?{ ??????????if(s?==?null?||?"".equals(s.trim()))?{ ??????????????return?true; ??????????} ??????????return?false; ??????????} ?????????? ??????}?
轉載于:https://www.cnblogs.com/jiezzy/archive/2012/04/21/2462191.html
總結
以上是生活随笔為你收集整理的getCacheDir用法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。