Android file operation

Source: Internet
Author: User

Http://www.cnblogs.com/LiHuiGe8/p/5604725.html read the assets file data in the asset directory is not directly accessible to the resources of the native resources, but this directory saved files can be packaged in the program, Android apps manage the raw resources in this directory through Assetmanager. Android does not generate IDs for files under assets, such as to use files in assets, you need to specify the path and file name of the file. Code Usage Example: (1) Create file in assets: Info.txt file content is: 1, this is a text 2 written using UTF-8, this is the second line of code 3, 3 lines 4, 4 lines
     //the contents of the read will change as the file changes.    Try {    //the read is a byte streamInputStream is=getresources (). Getassets (). Open ("Info.txt"); //It is important to specify the UTF-8 encoding.InputStreamReader isr=NewInputStreamReader (IS, "UTF-8"); BufferedReader BFR=NewBufferedReader (ISR); String in="";  while((In=bfr.readline ())! =NULL) {log.i (TAG, in); }//log.i (TAG, Bfr.readline ());}Catch(IOException e) {e.printstacktrace (); }

Third, readRawFile data in the directory if you want to save the static file in your application at compile time, save the file in your project res/raw/directory. The Android SDK processes the original resources in that directory and generates an index entry for the resources under that directory in the R manifest class. You can open it with Openrawresource (), by r.raw.<filename> the resource ID. This method returns the InputStream that you can use to read the file (but you cannot write to the original file).
 inputstream is=getresources (). Openrawresource (r.raw.info); InputStreamReader ISR  =new  inputstreamreader (IS, "UTF-8"  =new   BufferedReader ( ISR); String instring  = "" ;  while  ((Instring=bfr.readline ())!=null   
Four, read and write the internal storage of the file data program once the virtual machine or the real machine is running, the program will create a folder in a fixed place. The application's data file default location is saved in the:/data/data/<package name>/files  directory. <package name> represents the package name of the application.    fileinputstream openfileinput (String name): Opens the application's Data folder under the name file corresponding to the input stream. Fileoutputstream openfileoutput (String name,int mode): Opens the application's Data folder under the name file corresponding to the output stream. The parameter:mode   specifies the mode of the open file, which supports the following values:      mode_private : The file can only be read and written by the current program.      mode_append: Opens the file in Append mode, and the application can append the content to the file.      mode_world_readable: The contents of this file can be read by other applications.      mode_world_writeable: The contents of this file can be read and written by other programs.  getdir (String name,int mode): Gets or creates a subdirectory of name under the application's Data folder.  file getfilesdir (): Gets the absolute path of the data folder for the application.  string[] filelist (): Returns all files under the Data folder in the application.  deletefile (String): Deletes the specified file under the Data folder of the application.  2, file creation, deletion, renaming//creation of the file name public static final string file_name= "MyFile.txt"; (1) Create file code example: File file=new fIle (fileutil.file_name);         //file exists          if (!file.exists ())         {             try {                 //file does not exist, create a new file                  file.createnewfile ();                 system.out.println ("File already created");             } catch  (ioexception e)  {                  E.printstacktrace ();            }         }        else        {             system.out.println ("File already exists");             system.out.println ("File name:" +file.getname ());             system.out.println ("File absolute path is:" +file.getabsolutepath ());             //is present in the engineering catalogue, so              system.out.println ("File relative path is:" +file.getpath ());   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("File Size:" +file.length () + "bytes");             system.out.println ("File is readable:" + File.canread ());             system.out.println (" Whether the file can be written: "+file.canwrite ());   &Nbsp;         system.out.println ("I Do not Hide:" +file.isHidden ());         }  (2) Delete file example: File file=new file (fileutil.file_ NAME);         //file exists          if (file.exists ())         {             file.delete ();            &NBSP;SYSTEM.OUT.PRINTLN ("File has been deleted");        }  (3) Example of renaming files: file  file=new file (Fileutil.file_name); File newfile=new file ("AnotherFile.txt"); File.renameto (NewFile); SYSTEM.OUT.PRINTLN ("File has been successfully named" +file.getname ());          NOTE: When we rename a file, we only manipulate the file itself, The internal content does not change.  2, folder creation and deletion//Create folder name//public static final string folder_name= "NewFolder";//multi-level directory, not available /&Nbsp;   /*     * file.separator Path Separator       *  in the directory structure of a folder, it will not exist as long as any level of directory does not exist.      *  such as "NewFolder2" +file.separator+ "Separator2" This path, NewFolder2 does not exist,      *  so NewFolder2 and Separator2 don't exist      * */     public static final String FOLDER_NAME=              "NewFolder" +file.separator+ "Separator";  (1) Create folder code example File folder=new  file (Fileutil.folder_name);         if (!folder.exists ())          {             //Create a folder, it is impossible to exist once the same file or folder exists.             /*              * *  infolder in the directory structure, as long as any level of directory does not exist, then will not exist.              *  such as "NewFolder2" + file.separator+ "Separator2" This path, NewFolder2 does not exist,              *  so NewFolder2 and Separator2 don't exist               * *///             Folder.mkdir ();            /*              *  creates a folder slowly downward, regardless of whether the path exists.              *  so create a folder we generally use this method to determine stability.              * */             folder.mkdirs ();         }file can also represent files or folders   (2Delete Folder     file folder=new file (fileutil.folder_name);         if (Folder.exists ())         {             /*              *  at the time of removal, only the lowest-level directories will be removed, and no multi-level catalogs will be removed.              * */             folder.delete ();         }  

Android file Operation

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.