[ java ] in KIDS 글 쓴 이(By): soaa (소어아) 날 짜 (Date): 2001년 9월 7일 금요일 오후 12시 25분 51초 제 목(Title): Re: [Q]일종의 전역변수 같은 문제.. 제가 이해하는 게 맞다면.. 템플릿 처리 부분은 의외로 간단하게 처리될 수 있습니다. 아래 예제를 보세요.. Sigleton의 instance 를 얻어오는 메쏘드에서 template file의 last modified time만 체크하여 template file이 변경되었을 경우에 file을 로딩하면 됩니다. 아래 예제를 보시면 쉽게 이해할 수 있을 겁니다. 3초마다 TemplateLoader의 template 스트링을 찍는 예제입니다. D:\>java -classpath . Server Intializing Template Loader... template file is Modified reloading template file... Hello, World! Hello, World! Hello, World! template file is Modified reloading template file... Hello, World! java source: import java.io.*; public class Server { public static void main(String[] args) throws Exception { Server server = new Server(); server.goTest(); } public void goTest() throws Exception { while(true) { synchronized(this) { try { wait(3000); } catch(InterruptedException ie) {} TemplateLoader loader = TemplateLoader.getInstance(); System.out.println(loader.getText()); } } } } class TemplateLoader { private static TemplateLoader loader = null; private static String templateFname = "tf.txt"; private String template = ""; private static long lastModified = 0L; public static TemplateLoader getInstance() throws Exception { load(); return loader; } public static void load() throws Exception { if(loader == null) { System.out.println("Intializing Template Loader..."); loader = new TemplateLoader(); } File templateFile = new File(templateFname); if(!templateFile.exists()) throw new Exception("template file not exist"); if(templateFile.lastModified() != lastModified) { System.out.println("template file is Modified"); System.out.println("reloading template file..."); lastModified = templateFile.lastModified(); FileInputStream fi = null; StringWriter sw = null; try { fi = new FileInputStream(templateFile); sw = new StringWriter(); int c; while((c = fi.read()) > -1) { sw.write(c); } sw.flush(); loader.setText(sw.toString()); } catch(Exception ex) { throw ex; } finally { try { if(fi != null) fi.close(); if(sw != null) sw.close(); } catch(Exception e) {} } } } private TemplateLoader() { } public void setText(String text) { template = text; } public String getText() { return template; } } ----java source end .小 魚 兒 ............................. ><>. ><>. 그대 안의 ><>. 작은 물고기 .......................작은............ |