Solution for inconsistent file encoding causes file garbled characters

Source: Internet
Author: User

Solution for inconsistent file encoding causes file garbled characters

I have been working on the data encryption function of an android application over the past few days. To prevent the encryption and decryption algorithms from being cracked, I have encapsulated the core encryption and decryption algorithms in JNI, only expose the interface to the java layer.

The workflow is as follows:

1. Use a self-written encryption and decryption tool to encrypt data;

2. Place the encrypted data in the asserts folder of android;

3. When using data for the first time, copy the data in the asserts folder to a hidden folder;

4. decrypt the files in the hidden folder.


After the data is encrypted with the encryption tool, the program decrypts the data file, and finds that the decrypted file is twice the size of the original file, and all the files are garbled, the main problem occurs in step 1. The encoding method for read and write files is inconsistent, leading to file garbled characters. Previously, I used the following method to read the content in the asserts Folder:

public static String readFileFromAssets(Context context, String fileName) throws IOException {if (null == context || TextUtils.isEmpty( fileName )){return null;}AssetManager assetManager = context.getAssets();InputStream input = assetManager.open(fileName);ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length = 0;while ((length = input.read(buffer)) != -1) {output.write(buffer, 0, length);}output.close();input.close();return output.toString();}

The problem is that I converted the read content into a string. ByteArrayOutputStream's toString method converts the file content to UTF-8 encoding. However, by default, files read and written in C language are all encoded in asii, inconsistent encoding methods of read/write files cause garbled characters. If the problem is found, the solution is displayed as follows:

public static byte[] readFileFromAssets(Context context, String fileName) throws IOException {if (null == context || TextUtils.isEmpty( fileName )){return null;}AssetManager assetManager = context.getAssets();InputStream input = assetManager.open(fileName);ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length = 0;while ((length = input.read(buffer)) != -1) {output.write(buffer, 0, length);}output.close();input.close();return output.toByteArray( );//return output.toString();}

Return the content of the asserts file in byte array.



Java reads garbled files

This is because the encoding when you write the file is inconsistent with the encoding method when you read the file.
When you write a file, you do not specify the character encoding, but when you read the file content, you specify to read in UTF-8 encoding.
The read/write file encoding must be consistent to display it correctly.

You can use the default encoding to read files:
InputStreamReader read = new InputStreamReader (new FileInputStream (file ));

Java Chinese garbled

Because UTF-8 is encoded in linux, you may encounter garbled characters when using windos in linux. Similarly, if you use gb in a linux environment, it will be garbled, which is determined by the operating system. Therefore, you must use gb for windows. If you change it to another one, it will be garbled. Not supported by the operating system.

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.