自己用java写的统计代码行数的小软件。截图如下:
主要代码:
1.MainActivity.java
/**
*
*/
package com.ijustyce.lineCount;
import java.awt.Font;
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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* @author yc
*
*/
public class MainActivity extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static MainActivity main = new MainActivity();
private Font font = new IFont().getDefaultFont();
private int lineCount = 0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
main.setTitle("Lines of code");
main.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
main.setSize(320, 320);
main.setVisible(true);
}
public MainActivity() {
setLayout(new GridLayout(4, 1));
JButton addButton = new JButton("Browser directory");
addButton.addActionListener(this);
addButton.setFont(font);
add(addButton);
addButton = new JButton("Browser file");
addButton.addActionListener(this);
addButton.setFont(font);
add(addButton);
addButton = new JButton("clear count");
addButton.addActionListener(this);
addButton.setFont(font);
add(addButton);
addButton = new JButton("show lineCount");
addButton.addActionListener(this);
addButton.setFont(font);
add(addButton);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s = ((JButton) e.getSource()).getText();
if (s.equals("Browser directory")) {
fromDir();
return;
}
else if (s.equals("Browser file")) {
fromFile();
return;
}
else if (s.equals("show lineCount")) {
JOptionPane.showMessageDialog(null,"total : " + lineCount ,"Lines of code",
JOptionPane.INFORMATION_MESSAGE);
return;
}
else if(s.equals("clear count")){
lineCount = 0;
}
}
private void fromDir() {
Profile profile = new Profile();
String latestPath = (profile.read()?profile.latestPath:"C:");
JFileChooser chooser = new JFileChooser(latestPath);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().toString();
profile.write(path);
lineCount(path);
}
}
private void fromFile() {
Profile profile = new Profile();
String latestPath = (profile.read()?profile.latestPath:"C:/");
JFileChooser chooser = new JFileChooser(latestPath);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().toString();
profile.write(chooser.getSelectedFile().getParent());
lineCount(path);
}
}
private void lineCount(String path) {
File f = new File(path);
if(f.isDirectory()){
File list[] = f.listFiles();
for (File tmp : list) {
Count(tmp);
}
}
else{
Count(f);
}
JOptionPane.showMessageDialog(null,"total : " + lineCount ,"Lines of code",
JOptionPane.INFORMATION_MESSAGE);
}
private void Count(File f) {
try {
InputStream input = new FileInputStream(f);
BufferedReader b = new BufferedReader(new InputStreamReader(input));
String value = b.readLine();
while (value != null) {
lineCount++;
value = b.readLine();
}
b.close();
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.Profile.java
package com.ijustyce.lineCount;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
public class Profile {
String latestPath = "C:/";
File file = new File("set.ini");
boolean create() {
boolean b = true;
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
b = false;
}
return b;
}
boolean read() {
Properties pro;// 属性集
FileInputStream is = null;
boolean b = true;
if (!file.exists()) {// 配置文件不存在时
b = create();// 创建一个
if (b)// 创建成功后
b = write(latestPath);// 初始化
else
// 创建失败即不存在配置文件时弹出对话框提示错误
JOptionPane.showConfirmDialog(null, "对不起,不存在配置文件!", "错误",
JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
} else {
try {
is = new FileInputStream(file);
pro = new Properties();
pro.load(is);// 读取属性
latestPath = pro.getProperty("latestPath");// 读取配置参数latestPath的值
is.close();
} catch (IOException ex) {
ex.printStackTrace();
b = false;
}
}
return b;
}
boolean write(String latestPath) {
this.latestPath = latestPath;
Properties pro = null;
FileOutputStream os = null;
boolean b = true;
try {
os = new FileOutputStream(file);
pro = new Properties();
pro.setProperty("latestPath", latestPath);
pro.store(os, null); // 将属性写入
os.flush();
os.close();
System.out.println("latestPath=" + latestPath);
} catch (IOException ioe) {
b = false;
ioe.printStackTrace();
}
return b;
}
}
3.IFont.java
package com.ijustyce.lineCount;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
public class IFont {
/**
* create font from file
* @param fontFormat if font is trueType , fontFormat is Font.TRUETYPE_FONT
* else is Font.TYPE1_FONT
* @param path Font file path
* @param size font size
* @return font
*/
public Font getFont(int fontFormat, String path, float size) {
Font font = null;
try {
font = Font.createFont(fontFormat, new File(path));
font = font.deriveFont(size);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return font;
}
public Font getDefaultFont() {
Font font = null;
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File("msyh.ttc"));
font = font.deriveFont(18f);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return font;
}
}
源码下载:
http://download.csdn.net/detail/justyce/6730811