Java implementation txt file read, write operation instance code.

Source: Internet
Author: User

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.

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.