/**
* 读取文本文件
* JavaAlpha
*/
package counter;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class jtxtfm {
public static void main(String args[]) {
jtxtfrm fm = new jtxtfrm(); //这里是程序开始执行的地方,新建一个窗口
}
}
class jtxtfrm extends Frame implements ActionListener {
FileDialog op, sv;//文件对话框
Button btn1, btn2, btn3;//按钮
TextArea tarea;//文本框
jtxtfrm() {
super("统计代码工具");//标题
setLayout(null);//布局
setBackground(Color.white);//背景色
setSize(500, 300);//设置大小
setVisible(true);//设置可见
btn1 = new Button("统计");//创建按钮
btn2 = new Button("保存");
btn3 = new Button("关闭");
tarea = new TextArea("");//创建文本框
add(btn1);//添加按钮
add(btn2);
add(btn3);
add(tarea);
tarea.setBounds(30, 50, 350, 200);//设置文本框位置
btn1.setBounds(420, 60, 50, 30);
btn2.setBounds(420, 120, 50, 30);
btn3.setBounds(420, 180, 50, 30);
op = new FileDialog(this, "打开", FileDialog.LOAD);//新建对话框
sv = new FileDialog(this, "保存", FileDialog.SAVE);
btn1.addActionListener(this);//设置按钮点击监听事件
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(new WindowAdapter() {//关闭事件处理
public void windowClosing(WindowEvent e) {
setVisible(false);//设置不可见
System.exit(0);//程序关闭
}
});
}
public void actionPerformed(ActionEvent e) {
long normalLines = 0;
long totalLines=0;
long commentLines = 0;
long whiteLines = 0;
boolean comment = false;
if (e.getSource() == btn1) {//按钮1事件处理
op.setVisible(true);//设置可见
try {
File f1 = new File(op.getDirectory(), op.getFile());//打开文件
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);//读取文件
tarea.setText("");//设置文本框内容为空
String str="";
while ((str = br.readLine()) != null)
{
totalLines++;
str = str.trim();
if(str.matches("^[\\s&&[^\\n]]*$")) {
whiteLines ++;
} else if (str.startsWith("/*") && !str.endsWith("*/")) {
commentLines ++;
comment = true;
} else if (str.startsWith("/*") && str.endsWith("*/")) {
commentLines ++;
} else if (true == comment) {
commentLines ++;
if(str.endsWith("*/")) {
comment = false;
}
} else if (str.startsWith("//")) {
commentLines ++;
} else {
normalLines ++;
}
}
tarea.setFont(new Font("黑体",Font.BOLD,32));
tarea.append("空白行数"+whiteLines + '\n'+"注释行数"+commentLines+'\n'+"代码有效行数"+normalLines+'\n'+"代码总行数"+totalLines);//文本框添加读取的文件内容
fr.close();//关闭读取
} catch (Exception e1) {//如果有错误,这里进行处理
JOptionPane.showMessageDialog(null, "请输入统计文件!!!", "错误提示", JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(null, "文字", "标题", JOptionPane.QUESTION_MESSAGE);
e1.printStackTrace();//打印错误信息
}
}
if (e.getSource() == btn2) {//按钮1事件处理
sv.setVisible(true);
try {
File f1 = new File(sv.getDirectory(), sv.getFile());
FileWriter fw = new FileWriter(f1);
BufferedWriter bw = new BufferedWriter(fw);
String gt = tarea.getText();//获取文本框内容
bw.write(gt, 0, gt.length());//将文本框内容写入文件
bw.flush();//真正的写入文件
fw.close();//关闭写入
} catch (Exception e2) {//错误处理
e2.printStackTrace();//打印错误信息
}
}
if (e.getSource() == btn3) {//按钮3事件处理
System.exit(0);//关闭程序
}
}
}
西工大 软件工程 项目
5星 · 超过95%的资源 需积分: 10 130 浏览量
2013-06-23
01:28:48
上传
评论
收藏 13KB ZIP 举报
葫芦赛赛
- 粉丝: 132
- 资源: 11
最新资源
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
- 2001~2022年上市公司数字赋能指数.dta
- 2001~2022年上市公司数字赋能指数.xlsx
- 信息办公石大在线财务管理系统(含源码)-shidacaiwu.rar
- 信息办公电信计费系统完整代码-netctossconformity.rar
- matlab实现TD-SCDMA中初始同步捕捉DwPTS下行同步导频时隙的仿真.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈