Java reads the TXT file in the 2 method---and saves the content (each line is divided into 2 segments in a fixed character) into the map

Source: Internet
Author: User

#java读取txt文件的第一种方法

/*** Method: Readtxt * Function: Read TXT file and add the contents of the TXT file---each line as a string into the list * parameters: TXT file address * back: Map *@paramfile *@return     * @throwsIOException*/     Public StaticMap<string, string> readtxt (String file)throwsIOException {Map<string, string> tempmap =NewHashmap<string, string>(); List<String> Alllines =files.readalllines (paths.get (file)); //here is my interception of the contents of the file, a line divided into 2 paragraphs, the first paragraph is set to map the key, the second paragraph is set to the value of map         for(String line:alllines) {if(Line! = "")) {string[] temp= Line.split ("J::"); if(Temp.length = = 2) {tempmap.put (temp[0], temp[1]); }            }        }        returnTempmap; }

#读取txt文件的第二种方法

     PublicMap<string, string> Read ()throwsexception{//list<string> alllines = Files.readalllines (Paths.get (File));        /*In general, each read request made by the reader will cause a corresponding read request to the underlying character or byte stream. Therefore, it is recommended that BufferedReader be packaged on readers whose read () operations may be expensive, such as filereaders and Inputstreamreaders*/FileReader FileReader=NewFileReader (File); BufferedReader BufferedReader=NewBufferedReader (FileReader); Map<String,String> Tempmap =NewHashmap<string,string>();  while((lines = Bufferedreader.readline ())! =NULL) {list.add (lines);        } bufferedreader.close (); //here are the rules for me to cut, a line cut into 2 paragraphs, the first paragraph is set to map key, the second paragraph is set to the value of map         for(String singlelist:list) {if(Singlelist! = "") {string[] temp= Singlelist.split ("J::"); if(Temp.length = = 2) {tempmap.put (temp[0],temp[1]); }            }        }        returnTempmap; }

Java reads the TXT file in the 2 method---and saves the content (each line is divided into 2 segments in a fixed character) into the map

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.