JAVA Read file conversion character Set __java

Source: Internet
Author: User
Tags getmessage readline

Scene: Need to read a TXT file content, but the TXT is written using UTF-8, the development environment default character set is GBK.

Core code:

 Public arraylist<string[]> ReadData (String srcpath) {//TODO auto-generated method stub arraylist& Lt
        string[]> DataList = new arraylist<string[]> ();
        String errorpath = null;
        String backuppath = null;
        String receivedpath = null;

        File srcfile = null;
            try {backuppath = Adjustpath (Srcpath, "Backup");
            Errorpath = Adjustpath (Srcpath, "error");

            Receivedpath = Adjustpath (Srcpath, "received");
            string[] Infilenames = new File (receivedpath). List (); if (null!= infilenames && infilenames.length > 0) {for (String filename:infilenames) {//AS
                    The fruit folder has multiple files read String FilePath = receivedpath + file.separator + filename;
                    Srcfile = new File (FilePath);
                    System.out.println (Srcfile.length ());
                    InputStream fis = new FileInputStream (srcfile); Unicodereader ur = new Unicodereader (FIS, "UTF-8");
                    BufferedReader br = new BufferedReader (UR);
                    string[] Spliteddata = null;
                    String str = NULL; while (str = Br.readline ())!= null && str.length () > 0) {spliteddata = Splitstr (str
                        );
                    Datalist.add (Spliteddata);
                    } br.close ();
                    Ur.close ();

                Fis.close ();
            (FileNotFoundException e) {logger.error ("exception occurred" + E.getmessage ());
        E.printstacktrace ();
            catch (IOException IoE) {ioe.printstacktrace ();
        Logger.error ("Off Stream exception:" + Ioe.getmessage ());
    return DataList; }

The point is,
while ((str = br.readline ())!= null && str.length () > 0)

In this decision condition, Br.readline () is first assigned to STR
The character is read as Br.readline () executes this method
So it can't be written
while (Br.readline ())!= null && br.readline ()) > 0) {str = Br.readline ()}
In this case str reads out forever as null;

At the same time UTF-8 encoded format TXT file, beginning with the BOM, through this way to parse, the first line of the first word identifier.
You need to create a new reader subclass to refer to
http://blog.csdn.net/jackpk/article/details/5702964/

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.