After you copy the UTF-8 file with a Java program, it appears at the beginning.

Source: Internet
Author: User

The original file content is this:

1000|1.0.0313|20140814|2 Bank Serial number | transaction Time | Transaction date | Bank Virtual Account | Account number | account Type | name | gender | Voucher Type | ID no. | Certificate validity period | telephone | email | | Third-party product code 31301201408140000002802313000000|20140814101832|20140814|00000000002014081200000000000355| 6216920000065381|01|000|0|00|371321198812266128|20150202|15811111111| [Email protected]| | 11| | |8| 000888

We save it as UTF-8 format and read the file with the following Java code

Package Com.szkingdom.leejun;import Java.io.file;import Java.io.fileinputstream;import java.io.IOException;import Java.util.arrays;public class Filebomtest {public static void main (string[] args) throws IOException {File File = new file ("F:\\haha.txt"); FileInputStream fileinputstream=new fileinputstream (file); byte[] B=new byte[1024];byte[] b=new byte[0];int read =-1; while ((Read=fileinputstream.read (b)) >-1) {int i=b.length; B=arrays.copyof (B, B.length+read); for (int j=0;j<read;j++) {b[i+j]=b[j];}} System.out.println (New String (B, "UTF-8"));}}

This is how the console displays:

1000|1.0.0313|20140814|2 Bank Serial number | Transaction Time | Trading Date | Bank Virtual Account | Account number | account Type | name | gender | Voucher Type | ID no. | Certificate validity period | telephone | email | province | city | Third-party product code 31301201408140000002802313000000|20140814101832|20140814|00000000002014081200000000000355| 6216920000065381|01|000|0|00|371321198812266128|20150202|15811111111| [Email protected]| | 11| | |8| 000888

One more on the front? Why, this is why. The reason is actually very simple, when we re-save Windows under the last time Windows added BOM header to UTF-8. Causes a read exception to occur. We open the file with 16 binary to see: EF BB BF these several prefixes. Now that you know it's there? The principle of the number, then we just need to determine whether there is a prefix, if any, then skip the corresponding characters on the line. Or it can be read with an external tool class.


After you copy the UTF-8 file with a Java program, it appears at the beginning.

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.