programming/java

[java] 파일 템플릿 replace 소스

labj 2014. 9. 10. 10:09

프로젝트 템플릿을 제작하는데 원본 파일을 두고 replace만 할 수 있도록 하기 위한 기본 소스임



* templet source

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="200" height="63" valign="top" style="padding:10px 0px 0px 15px;"><img src="https://www.samsungdmcc.com/commons/images/sub_layoutbot_001.gif" width="200" height="63"></td>
    <td style="padding:10px 0px 10px 10px;"><table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td style="padding-bottom:5px;"> $footer1$<br></td>
      </tr>
      <tr>
        <td height="1px" class=" bgcol_darkgray"></td>
      </tr>
      <tr>
        <td height="10" style="padding-top:5px;">$footer2$</td>
      </tr>
      <tr>
        <td height="10" style="font-family:Arial; font-size:10px;">$footer3$</td>
      </tr>
    </table></td>
  </tr>
</table>





* java source

package com.jobtoy.sample1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class ex1 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        // D:\public\BoardController.java
        BufferedReader freader  = new BufferedReader( new InputStreamReader(new FileInputStream("D:/public/BoardController.java"),"utf-8") ) ;
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = freader.readLine()) != null) {
            sb.append(line+"\n");
        }
        freader.close();
        String emailTemplage = sb.toString();
        String emailBody = "";
       
        ///////////// template 에 내용적용  //////////////////////
        emailTemplage = emailTemplage.replaceAll("[$]footer1[$]", "test1");
        emailTemplage = emailTemplage.replaceAll("[$]footer2[$]", "test2");
        emailTemplage = emailTemplage.replaceAll("[$]footer3[$]", "test3");
       
        emailBody = emailTemplage.replaceAll("[$]mail_title[$]", "test4");
        emailBody = emailBody.replaceAll("[$]mail_contents[$]", "test5");
       
        System.out.println(emailBody);
       
    }

}

'programming > java' 카테고리의 다른 글

[java] md5 aes128 mysql 암호화 적용  (0) 2017.02.07
우분투 자바 설치  (0) 2016.07.11
[java] AES 암호화 샘플  (0) 2014.03.05
[java] commons-fileupload  (0) 2013.07.03
[java] commons-codec  (0) 2013.07.03