The LRC lyrics is a widely used lyrics file for parsing
Standard format: [minutes: seconds. milliseconds] Lyrics
1 ImportJava.io.BufferedReader;2 ImportJava.io.File;3 ImportJava.io.FileInputStream;4 ImportJava.io.InputStreamReader;5 Importjava.util.ArrayList;6 ImportJava.util.HashMap;7 Importjava.util.List;8 ImportJava.util.Map;9 ImportJava.util.Map.Entry;Ten ImportJava.util.regex.Matcher; One ImportJava.util.regex.Pattern; A - Public classTESTLRC { - the Public Static voidMain (string[] args) { -String Path = "D:\\A.LRC";//Lyrics file path -TESTLRC LRC =NewTESTLRC (); -List<map<long, string>> list =lrc.parse (path); + if(List! =NULL) { - PRINTLRC (list); + } A } at - /** - * Parse the LRC lyrics file - * - * @paramPath - * LRC file path in * @return - */ to PrivateList<map<long, string>>Parse (String path) { + //Store All lyrics information -List<map<long, string>> list =NewArraylist<map<long, string>>(); the Try { * //String encoding = "Utf-8";//character encoding, if it does not match the lyrics file encoding will appear garbled $String encoding = "GBK";Panax NotoginsengFile File =NewFile (path); - if(File.isfile () && file.exists ()) {//determine if a file exists theInputStreamReader Read =NewInputStreamReader ( + NewFileInputStream (file), encoding); ABufferedReader BufferedReader =NewBufferedReader (read); theString regex = "\\[(\\d{1,2}):(\\d{1,2}). (\\d{1,2}) \ \] "; +Pattern pattern = pattern.compile (regex);//Create a Pattern object -String Linestr =NULL;//reads a row of strings at a time $ while((Linestr = Bufferedreader.readline ())! =NULL) { $Matcher Matcher =Pattern.matcher (LINESTR); - while(Matcher.find ()) { - //a container for storing current time and text information theMap<long, string> map =NewHashmap<long, string>(); - //System.out.println (m.group (0));//example: [02:34.94]Wuyi //[02:34.94]----corresponds to---> [minutes: Seconds. milliseconds] theString min = matcher.group (1);//minutes -String sec = Matcher.group (2);//seconds WuString Mill = Matcher.group (3);//milliseconds, and note that this is actually multiplied by ten. - LongTime = Getlongtime (min, sec, Mill + "0"); About //gets the text content of the current time $String Text =linestr.substring (Matcher.end ()); - //System.out.println ("text-->" + text); -Map.put (time, text);//Add to Container - list.add (map); A } + } the read.close (); - returnlist; $}Else { theSYSTEM.OUT.PRINTLN ("The specified file cannot be found:" +path); the } the}Catch(Exception e) { theSYSTEM.OUT.PRINTLN ("Error reading file contents!")); - e.printstacktrace (); in } the return NULL; the } About the /** the * Converts the minutes, seconds, and milliseconds given as a string into a long number in milliseconds the * + * @parammin - * minutes the * @paramsecBayi * seconds the * @paramMill the * milliseconds - * @return - */ the Private Longgetlongtime (String min, string sec, String mill) { the //turn into integral type the intm =integer.parseint (min); the ints =Integer.parseint (sec); - intms =Integer.parseint (mill); the the if(S >= 60) { theSystem.out.println ("Warning: An incorrect item has occurred for one second--" + min + ":" + sec + "."94+ mill.substring (0, 2) + "]"); the } the //The time in milliseconds that is combined into a long integer the LongTime = m * * + + S * 1000 +MS;98 returnTime ; About } - 101 /**102 * Print lyrics information103 */104 Private Static voidPRINTLRC (List<map<long, string>>list) { the if(List! =NULL&&List.isEmpty ()) {106System.out.println ("No lyrics information!") ");107 }108 for(Map<long, string>map:list) {109 for(Entry<long, string>Entry:map.entrySet ()) { theSystem.out.println ("Time:" + entry.getkey () + "\ T lyrics:"111+Entry.getvalue ()); the }113 } the } the}
Java uses regular expressions to parse the LRC lyrics file