BKJIA Editor's note: Android consists of four main components: Activity, ContentProvider, Service, and Intent. To run Android files, you need to read and write files from four components. This article describes how to read and write Android files. It is helpful for users who are developing Android.
BKJIA recommended reading: Android development and application details
File storage location
In Android, the file I/O is stored in the/data/<package name>/file/filename directory.
Tip: Android is based on linux. In a linux file system, there is no disk partition similar to Windows. It starts with a forward slash.
Get input and output streams in Android
In Android, stream operations are very simple. There are two methods in the Context class to directly obtain the file input and output stream:
- public FileInputStream openFileInput (String name)
- public FileOutputStream openFileOutput (String name, int mode)
As the name suggests, you can get the input and output stream of the file through the above method. There are four modes for the mode in the second method:
◆ Use 0 or MODE_PRIVATE (the default operation): Use 0 to indicate the default value. Only the application that creates the file can access the file. Each file write overwrites the file.
◆ MODE_APPEND to append to an existing file: The append method is used for each file write, similar to the append method in StringBuffer.
◆ MODE_WORLD_READABLE: only read permission.
◆ MODE_WORLD_WRITEABLE: only write permission.
Tip: If you want to get the read and write permissions at the same time, you can create them in mode as follows:
- MODE_WORLD_READABLE+ MODE_WORLD_WRITEABLE
Supplement Java SE
FileOutputStream:
Public void write (byte [] B) throws IOException this method can write the specified byte array to the file output stream
FileInputStream:
Public int read (byte [] B) throws IOException this input stream reads data of up to B. length bytes into a byte array. This method is blocked before some input is available.
You can directly use the write method for the output stream. refer to the following code:
Java code
- <Span style = "white-space: pre;"> </span> /**
- * Write data
- * @ Param fs
- * @ Param content
- */
- Public void fileWrite (FileOutputStream fos, String content ){
- Byte [] contentcontentByteArray = content. getBytes ();
- Try {
- Fos. write (contentByteArray );
- } Catch (IOException e1 ){
- E1.printStackTrace ();
- }
- Try {// close the stream
- Fos. close ();
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- }
-
For the performance of the input stream, you can use ByteArrayOutputStream to create a character array in the memory. After reading the file, read the data. See the following code:
Java code
- * Read data
- * @ Param FCM
- * @ Return
- */
- Public String fileRead (FileInputStream ISI ){
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
- Byte [] buffer = new byte [1024];
- Int len =-1;
- Try {
- While (len = (FS. read (buffer )))! =-1 ){
- Baos. write (buffer, 0, len );
- }
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- String result = new String (baos. toByteArray ());
- // System. out. println (result );
- Try {
- Baos. close ();
- FCM. close ();
- } Catch (IOException e ){
- E. printStackTrace ();
- }
- Return result;
- }
ByteArrayOutputStream: This class implements an output stream, where data is written into a byte array.
Public void write (byte [] B, int off, int len) writes the len bytes starting from offset off in the specified byte array to the output stream of this byte array.
Refer to the Code for downloading FileIO code.