|
- import java.awt.*;
- import java.awt.datatransfer.*;
- import java.io.*;
- import java.util.regex.*;
- public class ClipCodeCount {
- /**
- * 正则表达式主要是匹配多行注释
- */
- private static Pattern pattern = Pattern.compile("/\\*([\\s|\\S])+?\\*/");
- /**
- * 统计文本中的代码行数 1.用正则表达式出去块注释即多行注释 2.再将字符串按照回车符分割成字符串数组 3.除去空白行或者单行注释行
- *
- * @param content
- * 输入文本
- * @return 文本中代码行数
- */
- public static int getCodeCount(String content) {
- int rowCount = 0;
- Matcher matcher = pattern.matcher(content);
- content = matcher.replaceAll(" ");
- String ss[] = content.split("\n");
- for (String s : ss) {
- if (s.trim().length() > 0 && !s.trim().startsWith("//"))
- rowCount++;
- }
- return rowCount;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- Clipboard clipboard = Toolkit.getDefaultToolkit()
- .getSystemClipboard();
- Transferable t = clipboard
- .getContents(null);
- try {
- if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
- Integer count = getCodeCount((String) t
- .getTransferData(DataFlavor.stringFlavor));
- clipboard.setContents(new StringSelection(count.toString()), null);
- }
- } catch (UnsupportedFlavorException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
复制代码 |
|