Sort the contents of the text

 
com.itcast.demo05.Buffered Package ; 

Import the java.io. * ;
Import java.util.Collections ;
Import the java.util.HashMap ;

/ **
* @author newcityman
* @date 2019/7/28 - 18:22
* exercise: the contents of the text sort
* in accordance with (1,2,3 ...) order sorting
* analysis:
* 1, create a HashMap collection, key storage number, value of each line of text stored
* 2, create a character buffer input stream object constructor binding character input stream
* 3, to create a buffered output stream object character, character bound constructor output stream
* 4, a method using a character in the input stream buffer the readLine, read row by row text
* 5, the read text cut to obtain the serial number and text line
* 6, and the number of cut SUMMARY stored in the hashMap, key number is ordered automatically Arranging
* 7, traversing hashMap collection, each acquiring a key-value pair
* 8, each of the key-value pair, a stitching line of text
* 9, the spliced text character buffer using the write methods in the output stream, written to a file
* 10, the release of resources
* /
public class DemoTest {
public static void main (String [] args) throws IOException {
//. 1, create a HashMap set, key storage number, value stored text of each line
HashMap <String , String> Map = new new the HashMap <> () ;
// 2, creates a character buffer input stream object constructor binding character input stream
the BufferedReader br = new new the BufferedReader ( new new the FileReader ( "day18_IOAndProperties \\ in.txt")) ;
// 3, to create a buffered output stream object character, character bound constructor output stream
BufferedWriter, BW = new new BufferedWriter, ( new new FileWriter ( "day18_IOAndProperties \\ out.txt")) ;
//. 4, use of the input character buffer stream readLine, reads the text line by line
String Line ;
the while ((Line = br.readLine ())! = null) {
//. 5, the read text to be cut, and number of acquired line text
String [] = line.split STR ( " \\ . ") ;
// 6, the number of cut and the content stored in the hashMap, key number is ordered automatically Arranging
map.put (STR [ 0] , STR [ . 1]) ;
}
//. 7, traversal hashMap set, each acquiring a key-value pair for (String Key: map.keySet ()) { String value = as map.get (Key) ; //. 8, each of the key-value pair, a stitching line of text line = key + "." + value ; //. 9, the spliced version, write buffering method using character output stream, and writes the file bw.write (Line) ; bw.newLine () ; } bw.close ( ) ; br.close () ; } }












 

Guess you like

Origin www.cnblogs.com/newcityboy/p/11260414.html