现在的位置: 首页 > 综合 > 正文

java 文件读写

2013年09月05日 ⁄ 综合 ⁄ 共 2624字 ⁄ 字号 评论关闭
public class CodeGenerate {
 /**
  * 1  传入你要生成的类名   Company
  * 2  将模板文件(dist/model)   copy到 c:/dist 下,
  * 3  执行代码,对应的代码生产到你的project根目录中,project中要有对应的目录
  *    src/
  *       com/
  *           hxd/
  *            company/
  *                 bo
  *                 ctrl
  *                 impl
  *                 vo
  * 4  将你 Company的po中的属性 copy的 生成的CompanyForm中 并生成相应的setter getter
  * 5  将config.xml 中的代码copy到对应的struts-config.xml 中
  * 6  将 其他的java jsp 文件copy到对应目录中,jsp文件要在 page下创建一个company的目录,
  *    jsp就copy到这里
  * 7  改动针对该 模块的显示jsp
  * 8  run tomcat ^_^
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  CodeGenerate codeGenerate = new CodeGenerate("PmCheck");
  codeGenerate.createAllFile();
 }
 public CodeGenerate(String newFile){
  this.newFile=newFile;
 }
 String newFile = "";
 public void createFile(String beforeTxt, String oldClass, String afterTxt,
   String newClass) {
  String FileName = "C://dist//" + beforeTxt + oldClass + afterTxt;
  System.out.println(beforeTxt + oldClass + afterTxt);
  System.out.println("========" + FileName);
  File myFile = new File(FileName);
  if (!myFile.exists()) {
   System.err.println("Can't Find " + FileName);
   return;
  }
  FileReader filereader;
  try {
   filereader = new FileReader(myFile);
   String line = "";
   BufferedReader read = new BufferedReader(filereader);
   try {
    FileWriter fw = new FileWriter(beforeTxt + newClass + afterTxt);
    PrintWriter out = new PrintWriter(fw);
    String cc = "";
    while ((line = read.readLine()) != null) {
     cc = line.replaceAll("Commodity", newClass);
     cc = cc.replaceAll("commodity", this.trans(newClass));
     System.out.println(cc);
     out.println(cc);
     // fw.write(cc, 0, cc.length());
    }
    // ------------
    fw.flush();
    out.close();
    filereader.close();
    fw.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void createAllFile() {
  String oldFile = "Commodity";
  createFile("", oldFile, "Bo.java", newFile);
  createFile("", oldFile, "Action.java", newFile);
  createFile("", oldFile, "Decorator.java", newFile);
  createFile("", oldFile, "Form.java", newFile);
  createFile("", oldFile, "Impl.java", newFile);
  // ------------------------------
  createFile("add", oldFile, ".jsp", newFile);
  createFile("edit", oldFile, ".jsp", newFile);
  createFile("view", oldFile, ".jsp", newFile);
  createFile("show", oldFile, ".jsp", newFile);
  // ----------------------
  createFile("", "config", ".xml", newFile);
 }
 public String trans(String cc) {
  String head = cc.substring(0, 1);
  String tail = cc.substring(1, cc.length());
  System.out.println(head);
  System.out.println(head.toLowerCase() + tail);
  return head.toLowerCase() + tail;
 }

抱歉!评论已关闭.