Use java to read and replace some files

Source: Internet
Author: User

Public static void main (String [] args ){
Try {
String content = "<body> Test6 (url, "d:/a.html", content );
/* System. out. println ("======="
+ Content. subSequence (0, content. length ()-7 ));
System. out. println (content. subSequence (content. length ()-7,
Content. length ()));
Int startIndex = content. lastIndexOf ("</body> ");

System. out
. Println ("======= 2 ===" + content. substring (0, startIndex ));*/
} Catch (Exception e ){
E. printStackTrace ();
}
}

///////////////////////////

Public static void test6 (String path, String path1, String newContent ){
Try {
FileReader fr = new FileReader (path );
BufferedReader br = new BufferedReader (fr );
String node = null;
StringBuffer buffer = new StringBuffer ();
System. out. println ("======================= html content of the original file ================== ");
While (node = br. readLine ())! = Null ){
Buffer. append (node );
}
Int startIndex = buffer. indexOf ("<body> ");
Int endIndex = buffer. lastIndexOf ("</body> ");
If (startIndex! =-1 & endIndex! =-1 ){

StringBuffer str = buffer. replace (startIndex, endIndex,
NewContent );
File file = new File (path1 );
If (file. exists ())
File. delete ();
Else
File. createNewFile ();
FileWriter fileWriter = new FileWriter (path1 );
FileWriter. write (str. toString ());
FileWriter. flush ();
FileWriter. close ();
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

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.