- Import Java.io.File;
- Import java.io.IOException;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.Menu;
- Import Android.view.MenuItem;
- Import Android.widget.Toast;
- Public class Mainactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- File file=new file ("/mnt/sdcard/test"); Create a file
- if (!file.exists ()) {
- try {
- File.createnewfile ();
- } catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }Else {
- Toast.maketext (mainactivity. This,"created successfully! ", Toast.length_short). Show ();
- }
- File.delete (); //delete files
- }
- }
Don't forget to add permissions in manifest:
[HTML]View PlainCopy
- </Pre><pre class="html" name="code">< Uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
Note:
[Java]View PlainCopy
- File file=this.getfilesdir (); This directory is the default data store directory for the current application (/data/data/com.example.filedemo1/files)
- LOG.I ("Info", file.tostring ());
- File file=this.getcachedir ();
- //This directory is the location where the default cache file for the current application is stored (/data/data/com.example.filedemo1/cache)
- //Put some files that are not very important in this create use
- //If the phone is running out of memory, the system will automatically delete the data from the app's cache directory .
- LOG.I ("Info", file.tostring ());
- ///data/data/< Package name >app_imooc (/DATA/DATA/COM.EXAMPLE.FILEDEMO1/APP_IMOOC)
- File file=this.getdir ("Imooc", mode_private);
- /*mode_private
- * 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.
- * Mode_append
- * Mode checks whether the file exists, appends content to the file, or creates a new file
- * mode_world_readable
- * Indicates that the current file can be read by another application
- * mode_world_writeable
- * Indicates that the current file can be written by another file
- * */
- LOG.I ("Info", file.tostring ());
- File file=this.getexternalcachedir ();
- //Can get External storage location (/mnt/sdcard/android/data/com.example.filedemo1/cache)
- //The data in this location is the same as the built-in use
- //If the app is uninstalled, the data will be erased automatically.
- //If the developer does not follow this rule, do not put the data into/mnt/sdcard/android/data/com.example.filedemo1/cache
- data will not be erased after uninstallation, resulting in so-called data garbage
- LOG.I ("Info", file.tostring ());
Create a file in Android