Android Learning Files

Source: Internet
Author: User

I. Input/output stream

First, you need to understand what "flow" is. Intuitively, the flow is like water, there is no size problem, and the integrity problem is avoided. Non-streaming data transfer, such as you download a picture, you need to download the entire picture before you can use, and the flow is different, like water, you can use a cup, take a bucket can also be used. So, the flow is a mode of data transmission.
The input stream and the output stream, the difference is the out and in, is relative to the program. It is true that the input stream gets the data and the output stream is the output data. You want to complicate the problem, the input is the program to get external data, the output is the program to the external transmission of data, there is no necessary connection between the two, are the flow, the difference is the direction of different, that is, the program can only input stream and no output stream, or only the output stream and no input stream.
In addition, the benefit of the flow is that the receiver can process ahead, shorten the waiting time, and increase the speed. For example, you Youku watch video, not the entire video downloaded to play, but the next on-demand point. But not all applications, such as encrypted files, often require the entire encrypted file to be decrypted.

Summarized as follows:

1. A stream is a data transfer mode.

2. Out and into is relative to the program.

3. The benefit of streaming is that you can handle it without having to accept it all.

Second, Android file operation

Here is the code for the file operation I summarized myself.

 PackageCom.lintao.file;Importorg.apache.http.util.EncodingUtils;ImportAndroid.content.Context;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classFiledemo {/*** Read the file under the private folder, the operation of the stream will generally be through a byte[] array as buffer to dump, here * conjecture, real-time video playback is probably the case, because it can be a small piece of a small piece of read, can reduce the memory pressure, and do not need to read all the To play, you can play while reading * *@paramContext *@paramfilename * When filename is just a file name, the file under the private folder is manipulated, but if you pass the path past, you can manipulate the file under the other path *@return     * @throwsIOException*/     Public Staticstring Readappfile (context context, string fileName)throwsIOException {if(Context = =NULL|| FileName = =NULL|| Filename.equals ("")) {            return NULL; } Bytearrayoutputstream Stream=NewBytearrayoutputstream (); FileInputStream FileInputStream=Context.openfileinput (fileName); byte[] buffer =New byte[1024]; intLen =-1; /** Fis.read (buffer) indicates fis.read (BUFFER,0,BUFFER.LENGTH), which reads buffer starting from 0, * reads buffer *. length to B Uffer, until read 1024byte, less than 1024 will be read to the end, and then write buffer to the stream*/         while(len = fileinputstream.read (buffer))! =-1) {            //Stream.Write (buffer, 0, Len);        } stream.close (); Fileinputstream.close (); //close the input stream//Into a string,        returnstream.tostring (); }    /*** Write content to private files * *@paramContext *@paramfilename * When filename is just a file name, the file under the private folder is manipulated, but if you pass the path past, you can manipulate the file under the other path *@paramcontent * What to write *@return     * @throwsIOException*/     Public Static voidWriteappfile (context context, string fileName, string content)throwsIOException {if(Context = =NULL|| FileName = =NULL|| Filename.equals ("")) {            return; } FileOutputStream FileOutputStream=Context.openfileoutput (fileName, context.        Mode_append);        Fileoutputstream.write (Content.getbytes ()); Fileoutputstream.close (); //turn off the output stream    }    /*** Read the file data from the assets, the sample is to read a file string and return, the actual use can also read the file read to the SD card, the implementation of file replication, etc. *@paramContext *@paramFileName *@throwsIOException*/     Public Staticstring Readfromassets (context context, string fileName)throwsIOException {String res= ""; //get the asset data stream in the resourceInputStream InputStream =context.getresources (). Getassets (). open (FileName); intLength =inputstream.available (); byte[] buffer =New byte[length]; //read the data stream into buffer firstinputstream.read (buffer); //Converts buffer to string with utf-8 and assigns a valueres = encodingutils.getstring (buffer, "UTF-8"); returnRes; }    //Read the file     Public StaticString Readsdfile (String fileName)throwsIOException {String res= ""; File File=NewFile (fileName); FileInputStream FIS=Newfileinputstream (file); intLength =fis.available (); byte[] buffer =New byte[length];        Fis.read (buffer); Res= encodingutils.getstring (buffer, "UTF-8");        Fis.close (); returnRes; }    //Write a file     Public Static voidwritesdfile (String fileName, string write_str)throwsIOException {String res= ""; File File=NewFile (fileName); FileOutputStream Fos=Newfileoutputstream (file); byte[] bytes =write_str.getbytes ();        Fos.write (bytes);    Fos.close (); }}

1) test writeappfile and Readappfile with the following code:

Filedemo.writeappfile (This, "test.txt", "Test item"= Filedemo.readappfile (This, "Test.txt" ); LOG.V (tag,test);

You can see that the file has been generated under your app's private folder.

Output of the log,

2) test public static string Readfromassets (context context, string fileName);

The code is as follows:

String readfromassets = filedemo.readfromassets (This, "test.txt"); LOG.V (TAG, readfromassets);

The text.txt you just generated has been placed under the assets folder with the following results:


3) test public static void Writesdfile (string filename, string writecontent) and public static string Readsdfile (string filename )

The code is as follows:

Filedemo.writesdfile ("/sdcard/test.txt", "test"= Filedemo.readsdfile ("/sdcard/test.txt"); LOG.V (TAG, readfromsdfile);

You can see that the file has been generated,

Log as follows,

Android Learning Files

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.