项目期间,为了更方便统计代码量,写了个简单的小工具,做的比较粗糙,很多功能有待完善,现供大家一起分享,一起学习一起进步。
运行界面如下:
代码如下,由于注释加的比较少,不太方便阅读,表示很抱歉,以后慢慢加上。为了方便也可以去我的资源http://download.youkuaiyun.com/detail/itsource/4174149下载。
1、CodeCountTool类----------------------------------
package cn.com.pan.myJFrame;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import cn.com.pan.myJFrame.control.CodeCountToolControl;
public class CodeCountTool extends JFrame {
JTree tree = null;
int i=0;
DefaultMutableTreeNode root;
DefaultMutableTreeNode month[] = new DefaultMutableTreeNode[13];
SheZhiJPanel shezhipan = new SheZhiJPanel();
ImageIcon icon = new ImageIcon("images/right.png");
public CodeCountTool(){
Container con = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("日历记事本");
for(int i=1;i<=12;i++){
month[i] = new DefaultMutableTreeNode(""+i+"月");
root.add(month[i]);
}
for(int i=1;i<=12;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
for(int j=1;j<=31;j++){
month[i].add(new DefaultMutableTreeNode(""+j+"日"));
}
}
else if(i==4||i==6||i==9||i==11){
for(int j=1;j<=30;j++){
month[i].add(new DefaultMutableTreeNode(""+j+"日"));
}
}
else {
for(int j=1;j<=28;j++){
month[i].add(new DefaultMutableTreeNode(""+j+"日"));
}
}
}
CodeCountToolControl action = new CodeCountToolControl(this);
tree = new JTree(root);
tree.addTreeSelectionListener(action);
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout());
JScrollPane scrollpane_2 = new JScrollPane(tree);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane_2,shezhipan);
con.add(splitPane);
this.setVisible(true);
this.setIconImage(icon.getImage());
this.setBounds(100, 100, 530, 410);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public DefaultMutableTreeNode[] getMyMonth() {
return month;
}
public JTree getMyTree() {
return tree;
}
}
2、SheZhiJPanel类----------------------------------
package cn.com.pan.myJFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import cn.com.pan.myJFrame.control.CodeCountToolControl;
import cn.com.pan.myJFrame.control.MyListControl;
public class SheZhiJPanel extends JPanel {
public JComboBox getList1() {
return list1;
}
public JComboBox getList2() {
return list2;
}
public JTextArea getMyText() {
return text;
}
JButton savabutton = new JButton("保存日志");
JButton deletebutton = new JButton("删除日志");
JTextArea text = new JTextArea(20, 10);
JPanel pan = new JPanel();
private JPanel pan3,pan4,pan5,pan6,pan7,pan2,pan1;
private JLabel label1, label2, label3;
private JTextField textField1, textField2;
public JTextField getTextField1() {
return textField1;
}
private JButton button1, button2,startbutton;
private JComboBox list1,list2;
public SheZhiJPanel() {
pan.setLayout(new BorderLayout());
CodeCountToolControl action = new CodeCountToolControl(this);
MyListControl listaction = new MyListControl(this);
MyListControl listaction1 = new MyListControl(this);
label1 = new JLabel();
textField2 = new JTextField(6);
list2 = new JComboBox();
list2.addItem("开发员1");
list2.addItem("开发员2");
list2.addItem("开发员3");
list2.addItem("开发员4");
list2.addItem("开发员5");
list2.addItem("开发员6");
list2.setPreferredSize(new Dimension(80,30));
list2.addItemListener(listaction);
pan2 = new JPanel();
pan2.add(list2);
pan2.setBorder(new TitledBorder(new LineBorder(Color.lightGray),"姓名",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
textField1 = new JTextField(16);
button1 = new JButton("浏览");
button1.addActionListener(action);
savabutton.addActionListener(action);
pan3 = new JPanel();
pan3.add(textField1);
pan3.add(button1);
pan3.setBorder(new TitledBorder(new LineBorder(Color.lightGray),"源代码文件夹",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
pan1 = new JPanel();
pan1.add(pan2);
pan1.add(pan3);
list1 = new JComboBox();
list1.addItem(".java");
list1.addItem(".txt");
list1.addItem(".doc");
list1.addItem(".bat");
list1.addItem(".wps");
list1.setPreferredSize(new Dimension(290,20));
list1.addItemListener(listaction1);
pan4 = new JPanel();
button2 = new JButton("选择");
pan4.add(list1);
pan4.add(button2);
pan4.setBorder(new TitledBorder(new LineBorder(Color.lightGray),"源代码类型",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
pan5 = new JPanel(new GridLayout(2,1));
pan5.add(pan1);
pan5.add(pan4);
pan5.setBorder(new TitledBorder(new LineBorder(Color.lightGray),"统计设置",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
deletebutton.addActionListener(action);
text.setSize(100, 30);
JScrollPane scrollpane_1 = new JScrollPane(text);
scrollpane_1.setPreferredSize(new Dimension(80,60));
pan6 = new JPanel();
startbutton = new JButton("开始统计");
startbutton.setPreferredSize(new Dimension(80,30));
startbutton.setMargin(new Insets(2,2,2,2));
startbutton.addActionListener(action);
savabutton.setPreferredSize(new Dimension(80,30));
deletebutton.setPreferredSize(new Dimension(80,30));
savabutton.setMargin(new Insets(2,2,2,2));
deletebutton.setMargin(new Insets(2,2,2,2));
pan6.add(startbutton);
pan6.add(savabutton);
pan6.add(deletebutton);
pan6.setBorder(new TitledBorder(new LineBorder(Color.lightGray),"统计显示",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
pan7 = new JPanel(new GridLayout(2,1));
pan7.add(scrollpane_1);
pan7.add(pan6);
pan7.setBorder(new TitledBorder(new LineBorder(Color.lightGray),"统计结果",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
pan.add(pan7, BorderLayout.CENTER);
JPanel pan2 = new JPanel();
pan2.add(pan5);
pan.add(pan2, BorderLayout.NORTH);
this.add(pan);
this.setBounds(100, 100, 400,360);
this.setVisible(true);
}
public JButton getDeletebutton() {
return deletebutton;
}
public JButton getSavabutton() {
return savabutton;
}
public JButton getButton1() {
return button1;
}
public JButton getButton2() {
return button2;
}
public JButton getStartbutton() {
return startbutton;
}
public static void main(String args[]){
new SheZhiJPanel();
}
}
3、Remember类----------------------------------
package cn.com.pan.myJFrame;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class Remember extends JFrame implements TreeSelectionListener,
ActionListener {
JTree tree = null;
JTextArea text = new JTextArea(20,20);
int i=0;
DefaultMutableTreeNode root;
JButton b_sava = new JButton("保存日志");
JButton b_del = new JButton("删除日志");
DefaultMutableTreeNode month[] = new DefaultMutableTreeNode[13];
public Remember(){
Container con = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("日历记事本");
for(int i=1;i<=12;i++){
month[i] = new DefaultMutableTreeNode(""+i+"月");
root.add(month[i]);
}
for(int i=1;i<=12;i++){
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
for(int j=1;j<=31;j++){
month[i].add(new DefaultMutableTreeNode(""+j+"日"));
}
}
else if(i==4||i==6||i==9||i==11){
for(int j=1;j<=30;j++){
month[i].add(new DefaultMutableTreeNode(""+j+"日"));
}
}
else {
for(int j=1;j<=28;j++){
month[i].add(new DefaultMutableTreeNode(""+j+"日"));
}
}
}
b_sava.addActionListener(this);
b_del.addActionListener(this);
tree = new JTree(root);
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout());
JScrollPane scrollpane_1 = new JScrollPane(text);
pan.add(scrollpane_1,BorderLayout.CENTER);
JPanel pan2 = new JPanel();
pan2.add(b_sava);
pan2.add(b_del);
pan.add(pan2,BorderLayout.NORTH);
JScrollPane scrollpane_2 = new JScrollPane(tree);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane_2,pan);
tree.addTreeSelectionListener(this);
con.add(splitPane);
this.setVisible(true);
this.setBounds(100, 100, 460, 320);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void valueChanged(TreeSelectionEvent e) {
text.setText(null);
if(e.getSource() == tree){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if(node.isLeaf()){
String str = node.toString();
for(int i=0;i<=12;i++){
if(node.getParent() == month[i]){
try {
String temp = null;
File f = new File("F:\\Java编程学习\\代码量统计器\\codeCountTool\\src\\cn\\com\\pan\\myJFrame\\"+node.getParent().toString()+str+".txt");
System.out.println("********"+"F:\\Java编程学习\\代码量统计器\\codeCountTool\\src\\cn\\com\\pan\\myJFrame\\"+node.getParent().toString()+str+".txt");
f.mkdir();
System.out.println("--------"+f.getAbsolutePath());
File newfile = File.createTempFile(""+node.getParent().toString()+str, ".txt");
FileReader file = new FileReader(newfile);
BufferedReader in = new BufferedReader(file);
while((temp = in.readLine())!=null){
text.append(temp+'\n');
file.close();
in.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException er) {
er.printStackTrace();
}
}
}
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b_sava){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String str = node.toString();
if(node.isLeaf()){
try {
File f = new File(node.getParent().toString()+str+".txt");
FileWriter tofile = new FileWriter(f);
BufferedWriter out = new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.flush();
tofile.close();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
else if(e.getSource() == b_del) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
String str = node.toString();
if(node.isLeaf()){
File f = new File(node.getParent().toString()+str+".txt");
f.delete();
}
}
}
}
4、TiShiJFrame类----------------------------------
package cn.com.pan.myJFrame;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TiShiJFrame extends JFrame{
JFrame frame = new JFrame();
JPanel pan;
private JLabel label;
public TiShiJFrame(String str){
frame.setLayout(new BorderLayout());
pan = new JPanel();
pan.setLayout(new BorderLayout());
label = new JLabel("操作成功!");
label.setText(""+str);
pan.add(label,BorderLayout.CENTER);
frame.add(pan,BorderLayout.CENTER);
frame.setVisible(true);
frame.setBounds(260, 260,160,100);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String args[]){
new TiShiJFrame("str");
}
}
5、CodeCountLineFun类----------------------------------
package cn.com.pan.myJFrame.codeCountLineFun;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class CodeCountLineFun {
public static int totalCount = 0;
private static int annotationsCount = 0;
private static int spaceCount = 0;
public static int getSpaceCount() {
return spaceCount;
}
public static void setSpaceCount(int spaceCount) {
CodeCountLineFun.spaceCount = spaceCount;
}
public static int getTotalCount() {
return totalCount;
}
private static int efficientCount = 0;
public CodeCountLineFun(){
}
/**
* 统计某个文件夹下的Java文件的有效代码量
* 有效代码量=总代码行数-空行行数-注释行数
*
*/
public static int countefficientCountLine(File file,String str) {
int i = 0;
File f[] = file.listFiles();
BufferedReader br = null;
for (i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
countefficientCountLine(f[i],str);
} else if (f[i].isFile()) {
if (f[i].getName().endsWith(".java")) {
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(f[i])));
String stringLine = br.readLine();
while (stringLine != null) {
totalCount++;
if (stringLine.trim().equals("")) {
spaceCount++;
} else if (stringLine.trim().startsWith("/*")
|| stringLine.trim().startsWith("/**")
|| stringLine.trim().startsWith("//")
|| stringLine.trim().startsWith("*")
|| stringLine.trim().startsWith("*/")) {
annotationsCount++;
}
stringLine = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOClose(br);
}
}
}
}
efficientCount = totalCount - annotationsCount - spaceCount;
return efficientCount;
}
public static void IOClose(Closeable close) {
if (close != null) {
try {
close.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6、CodeCountToolControl类----------------------------------
package cn.com.pan.myJFrame.control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import cn.com.pan.myJFrame.CodeCountTool;
import cn.com.pan.myJFrame.SheZhiJPanel;
import cn.com.pan.myJFrame.TiShiJFrame;
import cn.com.pan.myJFrame.codeCountLineFun.CodeCountLineFun;
public class CodeCountToolControl implements ActionListener,TreeSelectionListener{
SheZhiJPanel shezhipan;
CodeCountTool codecountTool;
static DefaultMutableTreeNode node;
private static String name;
private static String regex;
public static String getRegex() {
return regex;
}
public static void setRegex(String regex) {
CodeCountToolControl.regex = regex;
}
public String getTheName() {
return name;
}
public void setTheName(String name) {
this.name = name;
}
public CodeCountToolControl(){}
public CodeCountToolControl(SheZhiJPanel shezhipan) {
this.shezhipan = shezhipan;
}
public CodeCountToolControl(CodeCountTool codecountTool) {
this.codecountTool = codecountTool;
}
public void actionPerformed(ActionEvent e) {
File file = null; // 接收文件
int result = 0; // 接收操作状态
JFileChooser fileChooser = new JFileChooser("F:\\"); //文件选择框
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif",".java");
fileChooser.setFileFilter(filter);
if (e.getSource() == shezhipan.getButton1()) { //表示执行的是打开操作
shezhipan.getTextField1().setText(null); //打开将文字区域的内容清空
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置可选择文件夹
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
result = fileChooser.showOpenDialog(shezhipan);
if (result == JFileChooser.APPROVE_OPTION) { //选择的是确定按钮
file = fileChooser.getSelectedFile(); //得到选择的文件
shezhipan.getTextField1().setText("" + file.getAbsolutePath());
} else if (result == JFileChooser.CANCEL_OPTION) {
shezhipan.getTextField1().setText("没有选择任何文件");
} else {
shezhipan.getTextField1().setText("操作出现错误");
}
if (file != null) {
try {
Scanner scan = new Scanner(new FileInputStream(file));
scan.useDelimiter("\n");
while (scan.hasNext()) {
shezhipan.getMyText().append(scan.next());
shezhipan.getMyText().append("\n");
}
scan.close();
} catch (Exception e1) {
}
}
}
if (e.getSource() == shezhipan.getSavabutton()) { //判断是否是保存操作
if(node==null){
new TiShiJFrame("请选择一个日期路径!");
}
else{
if (node.isLeaf()) {
String str = node.toString();
try {
File f = new File("allLog\\"+node.getParent().toString() + str
+ ".txt");
FileWriter tofile = new FileWriter(f,true);
BufferedWriter out = new BufferedWriter(tofile);
out.write(shezhipan.getMyText().getText().trim()+"\n");
out.newLine();
out.flush();
tofile.close();
out.close();
new TiShiJFrame("保存操作成功!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
else if(e.getSource() == shezhipan.getDeletebutton()) {
if(node==null){
new TiShiJFrame("没有选择日期路径!");
}
else{
String str = node.toString();
if(node.isLeaf()){
File f = new File("allLog\\"+node.getParent().toString() + str
+ ".txt");
f.delete();
new TiShiJFrame("日志删除成功!");
}
}
}
if (e.getSource() == shezhipan.getStartbutton()) {
File f = new File(shezhipan.getTextField1().getText());
shezhipan.getMyText().setText(name+
" 纯代码数:" + CodeCountLineFun.countefficientCountLine(f,regex)
+ " 总代码数:" + CodeCountLineFun.totalCount
+ " 日期:"
+ CodeCountToolControl.dateToStr(new Date()));
}
}
public static String dateToStr(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return formatter.format(date);
}
public void valueChanged(TreeSelectionEvent e) {
if (e.getSource() == codecountTool.getMyTree()) {
node = (DefaultMutableTreeNode) codecountTool
.getMyTree().getLastSelectedPathComponent();
if (node.isLeaf()) {
String str = node.toString();
for (int i = 0; i <= 12; i++) {
if (node.getParent() == codecountTool.getMyMonth()[i]) {
try {
String temp = null;
File f = new File("allLog/"+node.getParent().toString()+str+".txt");
new File(f.getParent()).mkdirs();
f.createNewFile();
FileReader file = new FileReader(f);
BufferedReader in = new BufferedReader(file);
while((temp = in.readLine())!=null){
shezhipan.getMyText().append(temp+'\n');
file.close();
in.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException er) {
er.printStackTrace();
}
}
}
}
}
}
}
7、MyListControl类----------------------------------
package cn.com.pan.myJFrame.control;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import cn.com.pan.myJFrame.SheZhiJPanel;
public class MyListControl implements ItemListener {
SheZhiJPanel shezhipan;
CodeCountToolControl control = new CodeCountToolControl();
public MyListControl(SheZhiJPanel shezhipan){
this.shezhipan = shezhipan;
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == shezhipan.getList2()){
control.setTheName(""+shezhipan.getList2().getSelectedItem().toString());
}
if(e.getSource() == shezhipan.getList1()){
CodeCountToolControl.setRegex(""+shezhipan.getList1().getSelectedItem().toString().trim());
}
}
}
8、Test类----------------------------------
package cn.com.pan.myJFrame.codeCountLineTest;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import cn.com.pan.myJFrame.CodeCountTool;
public class Test {
public static void main(String args[]){
new CodeCountTool();
try {
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceFieldOfWheatLookAndFeel");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}