Create a file in Android

Source: Internet
Author: User

  1. Import Java.io.File;
  2. Import java.io.IOException;
  3. Import android.app.Activity;
  4. Import Android.os.Bundle;
  5. Import Android.view.Menu;
  6. Import Android.view.MenuItem;
  7. Import Android.widget.Toast;
  8. Public class Mainactivity extends Activity {
  9. @Override
  10. protected void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.activity_main);
  13. File file=new file ("/mnt/sdcard/test"); Create a file
  14. if (!file.exists ()) {
  15. try {
  16. File.createnewfile ();
  17. } catch (IOException e) {
  18. //TODO auto-generated catch block
  19. E.printstacktrace ();
  20. }
  21. }Else {
  22. Toast.maketext (mainactivity. This,"created successfully!  ", Toast.length_short). Show ();
  23. }
  24. File.delete (); //delete files
  25. }
  26. }


Don't forget to add permissions in manifest:

[HTML]View PlainCopy
    1. </Pre><pre class="html" name="code">< Uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>


Note:

[Java]View PlainCopy
    1. File file=this.getfilesdir (); This directory is the default data store directory for the current application (/data/data/com.example.filedemo1/files)
    2. LOG.I ("Info", file.tostring ());
    3. File file=this.getcachedir ();
    4. //This directory is the location where the default cache file for the current application is stored (/data/data/com.example.filedemo1/cache)
    5. //Put some files that are not very important in this create use
    6. //If the phone is running out of memory, the system will automatically delete the data from the app's cache directory .
    7. LOG.I ("Info", file.tostring ());
    8. ///data/data/< Package name >app_imooc (/DATA/DATA/COM.EXAMPLE.FILEDEMO1/APP_IMOOC)
    9. File file=this.getdir ("Imooc", mode_private);
    10. /*mode_private
    11. * As the default mode of operation, the representative file is private data and can only be accessed by the app itself, in which the content of the source file is overwritten.
    12. * Mode_append
    13. * Mode checks whether the file exists, appends content to the file, or creates a new file
    14. * mode_world_readable
    15. * Indicates that the current file can be read by another application
    16. * mode_world_writeable
    17. * Indicates that the current file can be written by another file
    18. * */
    19. LOG.I ("Info", file.tostring ());
    20. File file=this.getexternalcachedir ();
    21. //Can get External storage location (/mnt/sdcard/android/data/com.example.filedemo1/cache)
    22. //The data in this location is the same as the built-in use
    23. //If the app is uninstalled, the data will be erased automatically.
    24. //If the developer does not follow this rule, do not put the data into/mnt/sdcard/android/data/com.example.filedemo1/cache
    25. data will not be erased after uninstallation, resulting in so-called data garbage
    26. LOG.I ("Info", file.tostring ());

Create a file in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.