java统计单个文件行数

package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class LineNum extends JFrame{
private JPanel topPanel = new JPanel();
private JPanel bottomPanel = new JPanel();
private JButton fileChoose = new JButton("选择目录");
private JTextField fileField = new JTextField(20);
private JFileChooser fc = new JFileChooser("选择目录");
private JTextArea filePathArea = new JTextArea(5 , 20);

/**普通行数*/
private long normalLines=0;
/**注释行数*/
private long commentLines=0;
/**空白行数*/
private long spaceLines=0;
/**总行数*/
private long totalLines=0;

private DecimalFormat myFormat = null;

public LineNum(String title)
{
super(title);
//设置面板
Container container = getContentPane();
container.setLayout(new BorderLayout());
topPanel.setLayout(new GridLayout(1 , 2));
bottomPanel.setLayout(new BorderLayout());
topPanel.add(fileChoose);
topPanel.add(fileField);
bottomPanel.add(new JScrollPane(filePathArea));
container.add(topPanel , BorderLayout.NORTH);
container.add(bottomPanel , BorderLayout.CENTER);

//添加选择目录监听,默认获取的是选择文件所在的父目录,程序统计对象是此父目录及其子目录下的所有java文件
fileChoose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int result = fc.showOpenDialog(LineNum.this);
if(result == JFileChooser.APPROVE_OPTION)
{
String path = fc.getSelectedFile().getAbsolutePath();
fileField.setText(path);
File file = new File(path);
CalculateLineNum(file);
}
}
});
}

/*
* 计算并显示统计信息
*/
private void CalculateLineNum(File file)
{
if(file.exists())
{
this.setNormalLines(0L);
this.setCommentLines(0L);
this.setSpaceLines(0L);
this.setTotalLines(0L);
displayLineNum(file);
myFormat = (DecimalFormat)NumberFormat.getPercentInstance();
myFormat.applyPattern("0.00%");
totalLines=normalLines+spaceLines+commentLines;
filePathArea.setText("");
filePathArea.setText(filePathArea.getText() + "\n" + " 总行数:" + totalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 程序行数:" + normalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 注释行数:" + commentLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 空行行数:" + spaceLines + "行");

}
}

//循环访问目录及子目录,统计代码总行数,注释行数及空行行数
public void displayLineNum(File file)
{
BufferedReader br=null;
boolean comment=false;
try {
br=new BufferedReader(new FileReader(file));
String line="";
while((line=br.readLine())!=null){
line=line.trim();//去除空格
//空白
if(line.matches("^[\\s&&[^\\n]]*$")) {
spaceLines ++;
}
// //
else if(line.startsWith("//")) {
commentLines ++;
}
// 有/*的情况
else if(line.indexOf("/*") != -1)
{
// /*开始的情况
if(line.startsWith("/*"))
{
// 同一行里有*/
if(line.indexOf("*/") != -1)
{
//以*/结尾
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
}
else
{
commentLines ++;
comment = true;
}

}
// /*不是开始的情况
else
{
//同一行里没有*/
if(line.indexOf("*/") == -1)
{
comment = true;
normalLines ++;
}
}
}
else if(true == comment)
{
//是否有*/
if(line.indexOf("*/") != -1)
{
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
comment = false;
}
else
{
commentLines ++;
}
}
else
{
normalLines ++;
}
}

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

}

public static void main(String args[])
{
LineNum lineFrame=new LineNum("java程序行数统计");
lineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lineFrame.setBounds(212,159,600,420);
lineFrame.setVisible(true);
lineFrame.setResizable(false);
}

public long getNormalLines() {
return normalLines;
}

public void setNormalLines(long normalLines) {
this.normalLines = normalLines;
}

public long getCommentLines() {
return commentLines;
}

public void setCommentLines(long commentLines) {
this.commentLines = commentLines;
}

public long getSpaceLines() {
return spaceLines;
}

public void setSpaceLines(long spaceLines) {
this.spaceLines = spaceLines;
}

public long getTotalLines() {
return totalLines;
}

public void setTotalLines(long totalLines) {
this.totalLines = totalLines;
}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值