Summary of Android file reading and writing operation methods

Summary of Android file reading and writing operation methods

Files in Android are placed in different locations, and their reading methods are also different.

This paper sorts out the ways and methods of reading resource files, data area files, SD card files and random access files in Android. For reference.

1、 Reading of resource file:

1) Read file data from raw of resource:

2) Read file data from resource's asset

2、 Read and write files in / data / data / < application name > Directory:

3、 Read and write files in SD card. That is, the files under the / MNT / sdcard / Directory:

4、 Use the file class to read and write files:

5、 In addition, the file class also has the following common operations:

6、 Read and write files using RandomAccessFile:

RandomAccessFile is flexible and has many functions. You can use a way similar to seek to jump to any position of the file and start from the file indicator

Start reading and writing at the current location.

It has two construction methods

Use case:

7、 When reading a resource file, can you jump to any location of the file in a way similar to seek and read the specified number of bytes from the specified location?

The answer is yes.

The following functions are available in both FileInputStream and InputStream:

From the above example, we can see that the skip function is somewhat similar to the seek operation in C language, but they are somewhat different.

It should be noted that:

1. The skip function always jumps from the current position. In practical application, we should also judge the return value of the function. 2. The read function always starts reading at the current position. 3. In addition, you can use the reset function to reset the current location of the file to 0, that is, the starting location of the file.

How do I get the current location of the file?

I didn't find the relevant functions and methods. I don't know how to get the current location of the file. It seems that it's not too important.

8、 How to get InputStream from FileInputStream?

9、 The size of APK resource file cannot exceed 1m. What if it exceeds 1m? We can copy this data to the data directory and then use it. The code for copying data is as follows:

Summary:

1. There are two resource files in APK, which are opened and used in two different ways.

Raw uses InputStream in = getresources(). Openrawresource (r.raw. Test); The asset uses InputStream in = getresources(). Getassets(). Open (filename);

These data can only be read, not written. More importantly, the file size in this directory cannot exceed 1m.

At the same time, it should be noted that when using InputStream, you need to add throws IOException after the function name.

2. The files in the SD card use FileInputStream and fileoutputstream to operate the files.

3. Files stored in the data area (/ data / data /...) can only be operated with openfileoutput and openfileinput.

Note that FileInputStream and fileoutputstream cannot be used for file operation.

4. RandomAccessFile class is limited to file operations and cannot access other IO devices. It can jump to any location of the file and read and write from the current location.

5. Both InputStream and FileInputStream can use skip and read (buffer, offset, length) functions to realize random reading of files.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>