The use of read files commonly used in Android is as follows

Source: Internet
Author: User

1. Read the file data from resource raw:
""; try{in= Getresources (). Openrawresource (r.raw.test);  Get the size of the data in.available ();  Byte[length]; //Read data in.read (buffer);" BIG5 "); //Close in.close ();} catch (Exception e) {e.printstacktrace ();}          
2. Read the file data from the resource asset
File name string res="";  try{in= Getresources (). Getassets (). open (fileName);  In.available (); Byte[length]; in.read (buffer); "UTF-8");} catch (Exception e) {e.printstacktrace ();}       
3. Read and write the files on the/data/data/< application name > directory:
Write DataPublicvoid WriteFile (String filename,string writestr)Throws ioexception{try{fileoutputstream fout =openfileoutput (FileName, mode_private); catch (Exception e) {e.printstacktrace ();} //read data public String readFile (string fileName)  Throws ioexception{string Res=new fileinputstream (Jsonflie); byte [] buffer = new byte[length];fin.read (buffer ), res = encodingutils.getstring (buffer, catch (Exception e) {e.printstacktrace ();} return res;}             
4. Read and write the files in the SD card. Which is the file under the/mnt/sdcard/directory.
Write data to files in SDPublicvoid Writefilesdcardfile (String filename,string write_str)Throws ioexception{try{fileoutputstream fout = new fileoutputstream (fileName); byte [] bytes = Write_str.getbytes (); fout.write (bytes); Fout.close ();} catch (Exception e) {e.printstacktrace ();}} //read files in SD public String readfilesdcardfile (string fileName) throws ioexception{string res=new fileinputstream (fileName); byte [] buffer = new byte[length];fin.read (buffer ), res = encodingutils.getstring (buffer, catch (Exception e) {e.printstacktrace ();} return res;}              
5. Use the file class to read and write files:
Read the filepublic string Readsdfile (string fileName)throws ioexception {File File = new File (FileName); FileInputStream FIS = new fileinputstream (file); byte [] buffer = new byte[length];fis.read (buffer ), res = encodingutils.getstring (buffer, //write file public void writesdfile (String fileName, String write_str) throws ioexception{file file = new file (fileName); FileOutputStream fos = new fileoutputstream (file);        


6. [Code] v. In addition, the file class has some of the following common operations:

String Name = File.getname (); Get the name of the file or folder:

String Parentpath = File.getparent (); Get the parent directory of a file or folder

String path = File.getabsoultepath ();//Absolute Road warp

String path = File.getpath ();//Relative Road warp

File.createnewfile ();//Build file

File.mkdir (); Create a folder

File.isdirectory (); Judging is a file or folder

file[] files = file.listfiles (); List all file and folder names under a folder

File.renameto (dest); Modify folder and file name

File.delete (); Delete a folder or file

1. There are two kinds of resource files in apk, which are used in two different ways.
Raw uses inputstream in = Getresources (). Openrawresource (R.raw.test);

Asset uses inputstream in = Getresources (). Getassets (). open (FileName);

This data can only be read and cannot be written. More importantly, the file size under this directory cannot exceed 1M.

Also, it is important to note that you need to add throws IOException to the function name when using InputStream.

2, the files in the SD card use FileInputStream and FileOutputStream for file operation.

3, stored in the data area (/data/data/.) Files can only be manipulated using Openfileoutput and Openfileinput.

Note You cannot use FileInputStream and FileOutputStream for file operations.

4, the Randomaccessfile class is limited to file operation, cannot access other IO devices. It can jump to any location of the file and start reading and writing from the current position.

Excerpted from T9team

The use of read files commonly used in Android is as follows

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.