First, demand: in the TXT file need to build the statement of varchar (XXX) inside the xxx multiplied by 4, and then in accordance with the original format for output.
The requirements are as follows:
After conversion such as:
The implementation code is as follows:
Packagecommingming;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.FileWriter; Public classTest {/*** Read the contents of the TXT file *@paramfile object to read *@returnReturn file Contents*/ Public StaticString txt2string (file file) {StringBuilder result=NewStringBuilder (); String Numstr; Integer i; String Newstr; BufferedWriter SC=NULL; Try{bufferedreader br=NewBufferedReader (NewFileReader (file));//constructs a BufferedReader class to read the fileString s =NULL; SC=NewBufferedWriter (NewFileWriter (NewFile ("E:/newyx.sql"))); while((s = br.readline ())! =NULL){//use the ReadLine method to read one line at a time if(S.indexof ("VARCHAR (")!=-1) {Numstr=s.substring (S.indexof ("(") +1, S.indexof (")"));//Intercept to a numberI=integer.parseint (NUMSTR);//To calculate the number of the interceptionNewstr=i.tostring (); S=s.replaceall (Numstr, NEWSTR);//Replace the original number with the calculated numberResult.append (System.getproperty ("line.separator") +s); Sc.write (System.getproperty ("Line.separator") +s); } Else{result.append (System.getproperty ("Line.separator") +s); Sc.write (System.getproperty ("Line.separator") +s); }} br.close (); Sc.close (); }Catch(Exception e) {e.printstacktrace (); } returnresult.tostring (); } Public Static voidMain (string[] args) {File file=NewFile ("e:/no primary key. SQL"); System.out.println (txt2string (file)); }}
Java implementation txt file read, write operation instance code.