原文: https://howtodoinjava.com/eclipse/create-eclipse-templates-for-faster-java-coding/

我们大多数使用 Eclipse IDE 编写代码的人,都必须使用mainsysout之类的快捷方式,然后点击CTRL+SPACE,它将快捷方式转换为public static void main(String[] args){…}System.out.println()。 这是一项非常有用的功能,每当我为项目或教程编写代码时,我就一直使用它。

好消息是,您也可以在此列表中添加自己的模板,并利用此功能。 例如,解析作为参数传递的 XML 字符串是非常普遍的要求。 解析此类 XML 字符串的代码始终几乎相同。 我们可以为其创建模板,然后在需要时使用其快捷方式。

如何创建新的 Eclipse 模板

要创建 XML 字符串解析的快捷方式,请按照以下步骤操作:

1)通过转到“Windows -> 首选项”打开“首选项”对话框

2)在左侧导航树上,转到“Java -> 编辑器 -> 模板”

3)预定义模板

创建 Eclipse 模板以加快 Java 编程 - 图1

Eclipse 预定义模板

4)按下“New…”按钮添加新模板。

5)填写以下模板信息并保存

创建 Eclipse 模板以加快 Java 编程 - 图2

建立新模板

6)使用CTRL + SPACE在任何 Java 源文件中使用模板

创建 Eclipse 模板以加快 Java 编程 - 图3

使用模板快捷方式

7)按下Enter,它将生成下面的代码。 请享用 !!

创建 Eclipse 模板以加快 Java 编程 - 图4

代替快捷方式的插入代码

您会看到它很有用。 现在,让我们记下一些可以直接使用的代码模板。

有用的 Eclipse 模板示例

1)IO 模板文件

以下模板对于读取或写入文件很有用。 他们使用 Java7 功能(如try-with-resources)自动关闭文件。 他们还使用 NIO2.0 中的方法来获取缓冲的读取器并读取文件。

a)从文件中读取文本

  1. ${:import(java.nio.file.Files,
  2. java.nio.file.Paths,
  3. java.nio.charset.Charset,
  4. java.io.IOException,
  5. java.io.BufferedReader)}
  6. try (BufferedReader in = Files.newBufferedReader(Paths.get(${fileName:var(String)}),
  7. Charset.forName("UTF-8"))) {
  8. String line = null;
  9. while ((line = in.readLine()) != null) {
  10. ${cursor}
  11. }
  12. } catch (IOException e) {
  13. // ${todo}: handle exception
  14. }

b)从列表中的文件中读取所有行

  1. ${:import(java.nio.file.Files,
  2. java.nio.file.Paths,
  3. java.nio.charset.Charset,
  4. java.util.List,
  5. java.util.ArrayList)}
  6. Lis<String> lines = new ArrayList<>();
  7. try{
  8. lines = Files.readAllLines(Paths.get(${fileName:var(String)}),
  9. Charset.forName("UTF-8"));
  10. }catch (IOException e) {
  11. // ${todo}: handle exception
  12. }
  13. ${cursor}

c)写入文件

  1. ${:import(java.nio.file.Files,
  2. java.nio.file.Paths,
  3. java.nio.Charset,
  4. java.io.IOException,
  5. java.io.BufferedWriter)}
  6. try (BufferedWriter out = Files.newBufferedWriter(Paths.get(${fileName:var(String)}),
  7. Charset.forName("UTF-8"))) {
  8. out.write(${string:var(String)});
  9. out.newLine();
  10. ${cursor}
  11. } catch (IOException e) {
  12. // ${todo}: handle exception
  13. }

2)XML I/O 模板

以下模板用于读取 xml 文件或字符串并返回 DOM。

a)将 XML 文件解析为文档

  1. ${:import(org.w3c.dom.Document,
  2. javax.xml.parsers.DocumentBuilderFactory,
  3. java.io.File,
  4. java.io.IOException,
  5. javax.xml.parsers.ParserConfigurationException,
  6. org.xml.sax.SAXException)}
  7. Document doc = null;
  8. try {
  9. doc = DocumentBuilderFactory.newInstance()
  10. .newDocumentBuilder()
  11. .parse(new File(${filename:var(String)}));
  12. } catch (SAXException | IOException | ParserConfigurationException e) {
  13. // ${todo}: handle exception
  14. }
  15. ${cursor}

b)将 XML 字符串解析为文档

  1. ${:import(org.w3c.dom.Document,
  2. javax.xml.parsers.DocumentBuilderFactory,
  3. org.xml.sax.InputSource,
  4. java.io.StringReader,
  5. java.io.IOException,
  6. javax.xml.parsers.ParserConfigurationException,
  7. org.xml.sax.SAXException)}
  8. Document doc = null;
  9. try {
  10. doc = DocumentBuilderFactory.newInstance()
  11. .newDocumentBuilder()
  12. .parse(new InputSource(new StringReader(${str:var(String)})));
  13. } catch (SAXException | IOException | ParserConfigurationException e) {
  14. // ${todo}: handle exception
  15. }
  16. ${cursor}

3)日志模板

以下模板对于创建记录器和记录消息很有用。 我使用的是 SLF4J,但是可以很容易地对其进行调整,以使用任何其他日志记录框架。

a)创建一个新的记录器

  1. ${:import(org.slf4j.Logger,
  2. org.slf4j.LoggerFactory)}
  3. private static final Logger LOGGER = LoggerFactory.getLogger(${enclosing_type}.class);

b)在放置调试日志之前检查调试范围

  1. if(LOGGER.isDebugEnabled())
  2. LOGGER.debug(${word_selection}${});
  3. ${cursor}

c)日志信息级别声明

  1. LOGGER.info(${word_selection}${});
  2. ${cursor}

d)记录错误

  1. LOGGER.error(${word_selection}${}, ${exception_variable_name});

e)记录错误并引发异常

  1. LOGGER.error(${word_selection}${}, ${exception_variable_name});
  2. throw ${exception_variable_name};
  3. ${cursor}

4)JUNIT 模板

a)Junit 的之前方法

  1. ${:import (org.junit.Before)}
  2. @Before
  3. public void setUp() {
  4. ${cursor}
  5. }

b)Junit 的之后方法

  1. ${:import (org.junit.After)}
  2. @After
  3. public void tearDown() {
  4. ${cursor}
  5. }

c)Junit 的之前类

  1. ${:import (org.junit.BeforeClass)}
  2. @BeforeClass
  3. public static void oneTimeSetUp() {
  4. // one-time initialization code
  5. ${cursor}
  6. }

d)Junit 的之后类

  1. ${:import (org.junit.AfterClass)}
  2. @AfterClass
  3. public static void oneTimeTearDown() {
  4. // one-time cleanup code
  5. ${cursor}
  6. }

请注意,可以为其他文件类型(例如 XML,JSP 等)定义这些模板。在给定的链接中可以找到更多模板:

http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates

http://eclipse.dzone.com/news/effective-eclipse-custom-templ

祝您学习愉快!