2016-03-03 84 views
0

我有一些我需要的特定代碼,以便能夠擁有某些我不想每次寫入的I/O內容,而我只是希望能夠添加一個Java類,以便它已經具有在那裏的代碼,我試圖做:如何爲eclipse編寫代碼模板?

 
/* 
ID: my_id 
PROG: ${filename} 
LANG: JAVA 
*/ 
import java.util.*; 
import java.io.*; 
import java.net.InetAddress; 

public class ${filename} { 

    static class InputReader { 
     private StringTokenizer st = null; 
     private BufferedReader br = null; 

     public InputReader(String fileName) throws Exception { 
      try { 
       br = new BufferedReader(new FileReader(fileName)); 
      } catch (Exception e) { 
       e.printStackTrace(System.err); 
      } 
     } 

     public InputReader(InputStream in) { 
      try { 
       br = new BufferedReader(new InputStreamReader(in), 32768); 
      } catch (Exception e) { 
       e.printStackTrace(System.err); 
      } 
     } 

     public String next() { 
      while (st == null || !st.hasMoreTokens()) { 
       try { 
        st = new StringTokenizer(br.readLine()); 
       } catch (Exception e) { 
        e.printStackTrace(System.err); 
       } 
      } 
      return st.nextToken(); 
     } 

     public int nextInt() { 
      return Integer.parseInt(next()); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     InetAddress addr = InetAddress.getLocalHost(); 
     String hostname = addr.getHostName(); 
     boolean isLocal = hostname.equals("paulpc"); 
     String location = null; 
     InputReader in = null; 
     PrintWriter out = null; 
     if (!isLocal) { 
      location = ${filename}.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 
      in = new InputReader(location + "/" + "${filename}.in"); 
      out = new PrintWriter(new FileWriter(location + "/" + "${filename}.out")); 
     } else { 
      in = new InputReader(System.in); 
      out = new PrintWriter(System.out); 
     } 
     solve(in, out); 
     out.close(); 
    } 

    public static void solve(InputReader in, PrintWriter out) { 

    } 
} 

基本上,這件事需要在XML,但我不知道如何得當寫,我還以爲寫$ {文件名}處處將做到這一點,但它不起作用。總而言之,我希望文件的名稱寫在我寫「$ {filename}」的地方,我該怎麼做?

+0

嘗試$ {file_name} – user2277872

+0

@ user2277872沒有幫助,eclipse本身說它無法解析該xml文件。 Intellij使這個如此簡單! – Pavel

回答

1

可以聲明一個模板變量是這樣的:

public class ${cursor}${type:newName} { 
    public ${type}() { 
     // constructor 
    } 
} 

現在,如果你使用它作爲一個模板,既type出現將你寫的東西,當你插入模板後編輯進行更新。