#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