代码统计器

我们平时写过很多代码,但在完成某个工程时,自己到底写了多少行代码呢?

此代码统计器在这里就可以发挥作用了,它可以计算单个java文件的行数,
也可以计算出某个目录下所有java文件的总代码行数。

其中不妥之外望大家多提意见!3Q

注:统计结果中不包含注释行和空格行


package test;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser.*;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.util.Vector;


public class CodeFrame extends JFrame {
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jTextField2 = new JTextField();
JButton jButton1 = new JButton();
JLabel jLabel4 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JButton jButton2 = new JButton();
public CodeFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setBounds(0, 0, 510, 530);
jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 20));
jLabel1.setText("代码统计器");
jLabel1.setBounds(new Rectangle(181, 27, 116, 36));
jButton1.addActionListener(new CodeFrame_jButton1_actionAdapter(this));
jButton2.addActionListener(new CodeFrame_jButton2_actionAdapter(this));
jButton3.setBounds(new Rectangle(256, 405, 106, 32));
jButton3.setText("统计目录");
jButton3.addActionListener(new CodeFrame_jButton3_actionAdapter(this));
this.getContentPane().add(jLabel1, null);
jButton2.setBounds(new Rectangle(111, 405, 103, 33));
jButton2.setText("统计文件");
jScrollPane1.setBounds(new Rectangle(51, 196, 409, 184));
jLabel4.setForeground(UIManager.getColor("Button.disabledForeground"));
jLabel4.setText("(如.java)");
jLabel4.setBounds(new Rectangle(388, 91, 79, 32));
jButton1.setBounds(new Rectangle(388, 143, 73, 28));
jButton1.setText("选择");
jTextField2.setBounds(new Rectangle(169, 143, 209, 29));
jLabel3.setText("请选择文件/目录:");
jLabel3.setBounds(new Rectangle(51, 142, 115, 30));
jTextField1.setBounds(new Rectangle(169, 91, 210, 33));
this.getContentPane().add(jTextField1);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jScrollPane1);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton3);
jScrollPane1.getViewport().add(jTextArea1);
jLabel2.setText("请输入文件类型:");
jLabel2.setBounds(new Rectangle(51, 91, 107, 34));
this.setVisible(true);

}

public static void main(String[] args) {
CodeFrame codeframe = new CodeFrame();

}


File name;
JButton jButton3 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
JFileChooser f = new JFileChooser();
f.setFileFilter(new FileFilter() {
String t = jTextField1.getText();
public boolean accept(File f) {
return f.getName().endsWith(t);

}

public String getDescription() {
return t;
}

});

f.showOpenDialog(null);
File ff = f.getSelectedFile();

name = ff.getAbsoluteFile();
jTextField2.setText(ff.getAbsolutePath());

}


//268,73,308,265,322,139,47,177,59,322,336,273,113,98=2800

public void jButton2_actionPerformed(ActionEvent e) {
if (name.getAbsolutePath().endsWith(".java")) {
try {
DataInputStream din = new DataInputStream(new FileInputStream(
name));
int i = 0;
int n1 = 0, n2 = 0, n = 0;
;
int num = 0;
String s = "";

while (true) {
if ((s = din.readLine()) != null) {
if (s.trim().equals("") || s.trim().startsWith("//")) {

} else {
i++;
if (s.trim().startsWith("/*") &&
!s.trim().endsWith("*/")) {
n1 = i;
} else if (!s.trim().startsWith("/*") &&
s.trim().endsWith("*/")) {
n2 = i;
n += n2 - n1 + 1;
} else if (s.trim().startsWith("/*") &&
s.trim().endsWith("*/")) {
n1 = n2 = 0;
n += n2 - n1 + 1;
}

}
} else {
break;
}
}
num = i - n;

din.close();
String result =
"\n*******************代码所占行数统计结果**********************\n";
result += "文件名:" + name.getName() + "\n";
result += "代码行数:" + num + "\n";
jTextArea1.setText(result);

} catch (Exception ex) {
ex.printStackTrace();
}
}

}

public void jButton3_actionPerformed(ActionEvent e) {
String dir = jTextField2.getText();
if (!dir.endsWith(".java")) {
getFile(new File(dir));
int sum = 0;
for (int ii = 0; ii < v.size(); ii++) {
File f = new File((String) v.get(ii));

try {
DataInputStream din = new DataInputStream(new
FileInputStream(f));
int i = 0;
int n1 = 0, n2 = 0, n = 0;

String s = "";
int num = 0;
while (true) {
if ((s = din.readLine()) != null) {
if (s.trim().equals("") || s.trim().startsWith("//")) {

} else {
i++;
if (s.trim().startsWith("/*") &&
!s.trim().endsWith("*/")) {
n1 = i;
} else if (!s.trim().startsWith("/*") &&
s.trim().endsWith("*/")) {
n2 = i;
n += n2 - n1 + 1;
} else if (s.trim().startsWith("/*") &&
s.trim().endsWith("*/")) {
n1 = n2 = 0;
n += n2 - n1 + 1;
}

}
} else {
break;
}
}
num = i - n;

din.close();
sum += num;
} catch (Exception ex) {
ex.printStackTrace();
}


}
String result =
"\n*******************代码所占行数统计结果**********************\n";
// result += "文件名:" + name.getName() + "\n";
result += "代码行数:" + sum + "\n";
jTextArea1.setText(result);


}
}


static Vector v = new Vector();
public static void getFile(File f) {
if (f.isFile()) {
if (f.getName().endsWith(".java")) {
v.add(f.getAbsolutePath());
}

} else {
File[] fs = f.listFiles();
for (int i = 0; i < fs.length; i++) {
getFile(fs[i]);
}
}
}

}


class CodeFrame_jButton3_actionAdapter implements ActionListener {
private CodeFrame adaptee;
CodeFrame_jButton3_actionAdapter(CodeFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}


class CodeFrame_jButton2_actionAdapter implements ActionListener {
private CodeFrame adaptee;
CodeFrame_jButton2_actionAdapter(CodeFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}


class CodeFrame_jButton1_actionAdapter implements ActionListener {
private CodeFrame adaptee;
CodeFrame_jButton1_actionAdapter(CodeFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值