How far can we go series (4)
Nonsense:
I don't know how many people like to write music.Code? I am one of them. In the past, when I was in the company, I expected to buy a better MP3 file and write some code to get my salary. Life is always not as beautiful as you think.ProgramThere are a lot of things that people need to deal with, instead of sitting in a comfortable chair, drinking coffee or green tea and knocking a few lines of code can solve.
Are Everyone easy to give up? I did. I read two chapters in this book last month and I put them on hold. Listening to VOA started last week, and the last three days were ruined. Continue today.
I have read an article in the garden about how to find the ultimate goal of my life: I spent an hour thinking, keeping my mind down, and listing tables. The author believes that in this way, the goal of each person's life can be found at the end. To start a different journey of life. I wonder if anyone has tried it.
Life is like throwing a coin. As long as we can catch it, there will be results.
Cut file:
Copy file + delete file = cut file?
Okay,Original method:
Public Static Boolean Movefile1 (string frompath, string topath) Throws Ioexception {file fromfile = New File (frompath); inputstream is = New Bufferedinputstream ( New Fileinputstream (fromfile); outputstream OS = New Bufferedoutputstream ( New Fileoutputstream ( New File (topath ))); Byte B [] = New Byte [( Int ) Fromfile. Length ()]; While (Is. Read (B, 0, B. Length )! =-1 ) {OS. Write (B, 0 , B. Length) ;}is. Close (); OS. Close (); Return Fromfile. Delete ();}
But we will not use the above method to implement it, Because file hasSolve this problem in your own way:
/** ** @ Param Frompath cut file path --> "D:/My Documents/www.txt "* @ Param Topath: Path of the path to be cut --> "D:/My Documents/test/www.txt" */ Public Static Void Movefile2 (string frompath, string topath) {file fromfile = New File (frompath); file tofile = New File (topath ); // A file with the same name exists. If (Tofile. exists ()){ // Truncation path Int Last = topath. lastindexof (file. separator)> topath. lastindexof ("\\")? Topath. lastindexof (file. pathseparator): topath. lastindexof ("\\" ); String path = Topath. substring (0 , Last ); // Backup File Name File filebak = New File (path + file. Separator + tofile. getname () + ". Bak"); // Backup File Name If (! Tofile. renameto (filebak) {filebak. Delete (); // Delete backup files of the same name } // Backup File Tofile. renameto (filebak );} // Cut Fromfile. renameto (tofile );}
Note thatRenametoIf false is returned, the operation fails and may affect subsequent operations. (For example, a file with the same name already exists)
When I get home from work and want to modify the configuration file,Original method:
/** ** @ Param Path config file path * @ Param Nodename modify node name * @ Param Modify value * @ Return Modify the number of nodes. If-1 is returned, the modification fails * @ Throws Ioexception */ Public Int Changeconfig (string path, string nodename, string value) Throws Ioexception {file config = New File (PATH ); If (! Config. exists () | nodename = Null ){ Return -1 ;} File configbak = New File (path + "_ Bak" ); // Read Bufferedreader reader = New Bufferedreader ( New Inputstreamreader ( New Fileinputstream (config ))); // Write Bufferedwriter writer = New Bufferedwriter ( New Outputstreamwriter ( New Fileoutputstream (configbak ))); // Node form String nodestart = "<" + nodename + ">" ; String nodeend = "</" + Nodename + ">" ; String STR; Int I = 0 ; // Read each row While (STR = reader. Readline ())! = Null ) {Str =Str. Trim (); If (Str. Equals ("") | STR = Null ){ Continue ;} // Match each row If (Str. startswith (nodestart )&& Str. endswith (nodeend )){ // .*? Any number of duplicates (? <= Exp) matches the position (? = Exp) match the position before exp STR = Str. replaceall ("(? <=> )(.*?) (? = <)", Value); I ++ ;} Writer. Write (Str + "\ T \ n" );} Writer. Close (); reader. Close (); // The stream can be operated only after it is closed // Delete the original configuration file Config. Delete (); // Update Configuration File Configbak. renameto (config ); Return I ;}
There is always a better solution in this world,Try to solve the problem by using Dom parsing in Java:
/** ** @ Param Path: Path of the configuration file * @ Param Nodename node name * @ Param Nodevalue to what value * @ Return * @ Throws Exception */ Public Int Changeconfig1 (string path, string nodename, string nodevalue) Throws Exception {file config = New File (PATH ); // Parser factory type Documentbuilderfactory DBF = Documentbuilderfactory. newinstance (); // Parser Documentbuilder DB = DBF. newdocumentbuilder (); // Document Tree Model document Document document = DB. parse (config ); // A. XML document Parsing Nodelist = Document. getelementsbytagname (nodename ); // B. Convert the XML document to be parsed into an input stream before parsing. // Inputstream is = new fileinputstream (config ); // Document nodelist = dB. parse (is ); For ( Int I = 0; I <nodelist. getlength (); I ++){ // Element extends Node Element Node = (Element) nodelist. item (I); system. Out. println (node. getelementsbytagname ( "B"). Item (0 ). Getfirstchild (). getnodevalue (); node. getelementsbytagname (nodename). Item ( 0 ). Getfirstchild (). settextcontent (nodevalue); system. Out. println (node. getelementsbytagname ( "C"). Item (0 ). Getfirstchild (). getnodevalue ());} // The modification takes effect. Many code on the Internet did not mention this step. I don't know how they modified the XML value. // It can be understood that the data in the memory has been changed and has not been mapped to the file. Transformerfactory tffac = Transformerfactory. newinstance (); transformer TF = Tffac. newtransformer (); domsource Source = New Domsource (document); TF. Transform (source, New Streamresult (config )); // Modify content ing to file Return 0 ;}
Today, when I was studying, I learned that education should start from a very small age and the better.ArticleI don't forget the skills I learned in my twenties for 20 years. I don't need to be new in a month. It seems that the bigger it is, the more useless it is! Don't stop, don't stop, It's Just regressing, because everyone around you is making progress.
----------------------------------------------------------------------
Hard work may fail, but not hard work will certainly fail.
Mutual encouragement.