نسخهٔ فعلی |
متن شما |
سطر ۱۶۴: |
سطر ۱۶۴: |
| </div> | | </div> |
| | | |
− | == External Storage ==
| |
| | | |
− | می توان داد ها را در دستگاه های جانبی مانند SD Card ذخیره نمود.
| |
| | | |
− | ولی هنگام write بر روی دستگاه های جانبی فایلی در این مسیر ایجاد خواهد شد:
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | /sdcard/Android/data/com.company.app/files/save/appdata/save_appdata
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | کد زیر را در نظر بگیرید:
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | private String filename = "myfile"
| |
− |
| |
− | private String string = "sensitive data such as credit card number"
| |
− | FileOutputStream fos = null;
| |
− |
| |
− | try {
| |
− | File file = new File(getExternalFilesDir(TARGET_TYPE), filename);
| |
− | fos = new FileOutputStream(file, false);
| |
− | fos.write(string.getBytes());
| |
− | } catch (FileNotFoundException e) {
| |
− | // handle FileNotFoundException
| |
− | } catch (IOException e) {
| |
− | // handle IOException
| |
− | } finally {
| |
− | if (fos != null) {
| |
− | try {
| |
− | fos.close();
| |
− | } catch (IOException e) {
| |
− | // handle error
| |
− | }
| |
− | }
| |
− | }
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | برای جلوگیری از این آسیب پذیری می توان از openFileOutput و حالت MODE_PRIVATE به صورت زیر استفاد نمود:
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | private String filename = "myfile"
| |
− | private String string = "sensitive data such as credit card number"
| |
− | FileOutputStream fos = null;
| |
− |
| |
− | try {
| |
− | fos = openFileOutput(filename, Context.MODE_PRIVATE);
| |
− | fos.write(string.getBytes());
| |
− | fos.close();
| |
− | } catch (FileNotFoundException e) {
| |
− | // handle FileNotFoundException
| |
− | } catch (IOException e) {
| |
− | // handle IOException
| |
− | } finally {
| |
− | if (fos != null) {
| |
− | try {
| |
− | fos.close();
| |
− | } catch (IOException e) {
| |
− | // handle error
| |
− | }
| |
− | }
| |
− | }
| |
− | </pre>
| |
− | </div>
| |
| | | |
| | | |
| [[category:ذخیره سازی ناامن داده(Insecure Data Storage)]] | | [[category:ذخیره سازی ناامن داده(Insecure Data Storage)]] |