Java统计代码行数
2013-12-17 20:24
399 查看
自己用java写的统计代码行数的小软件。截图如下:
主要代码:
1.MainActivity.java
2.Profile.java
3.IFont.java
源码下载:
http://download.csdn.net/detail/justyce/6730811
主要代码:
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
相关文章推荐
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- 统计java代码行数和jar包中*.class代码的行数
- 统计Java程序代码行数(Java)
- java统计代码行数
- 统计java代码行数的代码(除注释,空行)
- 分享统计Java web项目的行数的代码
- [Java] 正则表达式 02 EmailSpider 抓邮件, CodeCounter 统计程序有效代码行数
- 统计某个目录(含子目录)下所有java源文件中实际代码行数
- 统计java代码行数工具
- java 统计代码行数
- 分享统计Java web项目的行数的代码
- 统计你写的java代码行数
- Java代码行数的统计
- [Java] 正则表达式 02 EmailSpider 抓邮件, CodeCounter 统计程序有效代码行数
- 用java统计一个目录(含子目录)下所有java源文件中代码行数
- 统计java方法(函数)的代码行数
- [JAVA实用应用] 项目代码行数统计器,帮助你统计项目一共有多少行代码
- 统计你写的java代码行数
- 统计java代码文件的代码行数(空行、注释不能计数)
- 统计项目源码行数的Java代码