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/