Android-Chinese dictionary development-assets, raw inputstream data stream operations (File splitting and merging)

Source: Internet
Author: User
Tags sqlite db

File Movement

1. Some external files need to be placed in the assets or raw folder for further use in the application for various reasons. The two folders have the following differences and links:

1. All data is read in the form of data streams. As a result, other reading methods in Java cannot work well on these files, such as randomaccessfile and filereader. If you need to use a file-based class, you can create a temporary file (file. createtempfile) based on the data stream as a compromise. This is a method. The following describes another method.
2. Files in the raw folder cannot contain a directory structure and each file is mapped to an ID, while the assets folder can have a directory structure. Programs that are sensitive to file names use raw for external file storage, while files that are more dependent on directory structures use assets for storage.
3. The files in both folders cannot be too large. The official data is smaller than 1 MB. This must be kept in mind at all times, because the problem is very hidden, you can find this file in the program or generate inputstream, but the ioexception exception will be thrown during reading. In this case, large files must be split before reading.
4. these two folders are case sensitive to file names. The names should be in lower case as much as possible, and you should also pay attention to the file names after splitting and merging. Otherwise, the program will regard them as different files, however, the previous files will be overwritten during creation (this is too concealed ,~~~~ (>_< )~~~~ )

The following code separates data:

123456789101112131415161718192021222324
public static void CutFilesInSizeParts(InputStream fis,String OutputFileName, int MaxPartSize) {try { int TotalLength = fis.available();byte[] buffer = new byte[TotalLength + 1];int len = fis.read(buffer); int nbPart = len / MaxPartSize + 1;int CurPos = 0; for (int i = 0; i < nbPart; i++) {int PartLen = MaxPartSize;if (CurPos + PartLen >= len)PartLen = len - CurPos;String outRealFileName = OutputFileName + (i + 1);FileOutputStream fos = new FileOutputStream(outRealFileName);fos.write(buffer, CurPos, PartLen);CurPos += PartLen;}} catch (IOException e) {e.printStackTrace();}}

2. It is exactly the two folders that can only generate inputstream data streams. When the program has other needs, it will be powerless. For example, if an SQLite dB file is created outside, the file must be embedded in the APK and can only be placed in these two folders, however, you can place it in sdcard or database as needed. Therefore, you need to read the file and generate the target file in the corresponding location and read it as needed. This also provides the second method. Pay attention to the following points in this step:

1. File Permission, otherwise it may be used by other applications
2. If you want to merge the split files, pay attention to the file order.
The code for merging and copying data is attached here.

1234567891011121314151617181920212223242526
// Merge and copy the public static void createfromrawdbfiles (file [] filelist, fileoutputstream FOS) {try {for (File file: filelist) {inputstream inputfile = new fileinputstream (File ); int totallength = 0; try {totallength = inputfile. available ();} catch (ioexception e) {}// reading and writing the file Method 1: byte [] buffer = new byte [totallength]; int Len = 0; try {Len = inputfile. read (buffer);} catch (ioexception e) {} FOS. write (buffer, 0, Len); inputfile. close ();} FOS. close () ;}catch (ioexception e ){}}

Link: http://ishelf.javaeye.com/blog/724633

Related Article

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.