首页 文章

如何为eclipse编写代码模板?

提问于
浏览
0

我有一些我需要的特定代码,以便能够拥有我不想每次都写的某些I / O内容,而我只是想能够添加一个java类,以便它已经具有该代码在那里,我尝试过:

/*
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中,但我不知道如何正确编写它,我以为在任何地方编写$ 都会这样做,但它不起作用 . 总而言之,我希望文件的名称写在我写“$ ”的地方,我该怎么办呢?

1 回答

  • 1

    你可以像这样声明一个模板变量:

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

    现在,如果您将此作为模板使用,则在模板插入后编辑时,您所写的内容将更新 type 次 .

相关问题