There are four main components of Android: Activity, ContentProvider, Service, intent. The operation of the Android file mainly requires reading and writing four components of the file. This article will show you how to read and write Android files and want to be helpful to friends who are developing Android .
File storage location
The I/O for files in android is stored in the/data/data/<package name>/file/filename directory.
Tip: Android is a Linux-based system, and there is no Windows-like disk partitioning in the Linux file system, which begins with a forward slash "/".
Get input and output stream in Android
In Android, the operation for a stream is simple. In the context class there are two ways to directly get the file input and output stream:
1 public fileinputstream openfileinput (String name)
2 public fileoutputstream openfileoutput int mode )
As the name implies, the file input and output stream can be obtained by means of the above method. For mode in the second method, there are four modes:
Use 0 or mode_private (the default operation): uses 0 to indicate the default value, only the application that can create the file accesses the file, each time the file is written as an overlay.
Mode_append to APPEND to an existing file: Each time the files are written as append methods, similar to the APPEND () method in StringBuffer.
Mode_world_readable: only Read permission.
Mode_world_writeable: Only write permission.
Tip: If you want to have read and write permissions at the same time, you can create them in mode as follows:
mode_world_readable +
Additions to the Java SE section
FileOutputStream:
public void Write (byte[] b) throws IOException the method writes the specified byte array to the file output stream
FileInputStream:
public int read (byte[] b) throws IOException reads a maximum of b.length bytes of data from this input stream into a byte array. This method will block until some of the inputs are available.
For the output stream to use the Write method directly, you can refer to the following code:
Java code
1. <SPAN style="White-space:pre;"></span>/**
2. * Write Data
3. *@paramFS
4. *@paramContent
5.*/
6. PublicvoidfileWrite (fileoutputstream fos,string content) {
7. byte[] Contentcontentbytearray=content.getbytes ();
8. Try {
9. Fos.write (Contentbytearray);
Ten. } Catch(IOException E1) {
One. E1.printstacktrace ();
A. }
-. Try {//Close the stream
-. Fos.close ();
the. } Catch(IOException e) {
-. E.printstacktrace ();
-. }
-. }
For the input stream, for performance reasons, you can use Bytearrayoutputstream to create an array of characters into memory, and when you are finished reading the file, refer to the following code:
Java code
1. *Read Data
2. *@param fis
3. * @return
4. */
5. PublicString fileRead (FileInputStream fis) {
6. Bytearrayoutputstream BAOs=NewBytearrayoutputstream ();
7. byte[] Buffer=Newbyte[1024x768];
8. intLen=-1;
9. Try {
Ten. while(Len=(fis.read (buffer)))!=-1){
One. Baos.write (Buffer,0, Len);
A. }
-. } Catch(IOException e) {
-. E.printstacktrace ();
the. }
-. String result=NewString (Baos.tobytearray ());
-. //System.out.println (Result);
-. Try {
+. Baos.close ();
-. Fis.close ();
+. } Catch(IOException e) {
A. E.printstacktrace ();
at. }
-. returnresult;
-. }
Bytearrayoutputstream: This class implements an output stream in which the data is written to a byte array.
public void Write (byte[] b,int off,int len) writes this byte array output stream to a byte array that specifies the Len bytes starting at offset off.
Original: http://www.codesocang.com/jiaocheng/shoujikaifa/2013/0508/4479.html