Java generate files (omnipotent) according to your own needs

Source: Internet
Author: User

The following method can be used to output the desired file, depending on the situation. I will take the output html as an example below.
// Html Template
<Html>
<Head> <title> </Html>
 
<Body>
<Div >### content ###"
### Ccc ###
</Div>
</Body>
</Html>
// ------------------------------------------- End of the template k.html
File foutt = new File ("D: \ Nworkspace \ ytcms \ WebRoot \ html \ zs \ z.html"); // path of the output File
File finput = new File ("D :\\ Nworkspace \ ytcms \ WebRoot \ html \ zs \ k.html"); // read the File template path
String templateContent = "";
Try {
// Read the object [input file]
FileInputStream fiut = new FileInputStream (finput );
InputStreamReader iReader = new InputStreamReader (fiut, "UTF-8 ");
StringBuffer sb1 = new StringBuffer ();
While (iReader. ready ()){
Sb1.append (char) iReader. read ());
}
TemplateContent = sb1.toString (). replaceAll ("### content ###", "what is this "). replaceAll ("### ccc ###", "This is step on ");
System. out. println (templateContent );
IReader. close ();
Fiut. close ();
// Write a file [output file]
FileOutputStream fout = new FileOutputStream (foutt );
OutputStreamWriter oWriter = new OutputStreamWriter (fout, "UTF-8 ");

OWriter. append (templateContent );
OWriter. close ();
Fout. close ();
} Catch (FileNotFoundException e ){
// TODO automatically generates catch Blocks
E. printStackTrace ();
} Catch (UnsupportedEncodingException e ){
// TODO automatically generates catch Blocks
E. printStackTrace ();
} Catch (IOException e ){
// TODO automatically generates catch Blocks
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.