Share a small program that counts the amount of code

Source: Internet
Author: User
This article mainly shares the relevant information about a small program that counts the amount of code and has some reference value. if you are interested, you can refer to it and run it directly: Select the src folder, just click "count the number of rows ".

Import java. awt. eventQueue; import java. awt. font; import java. awt. event. mouseAdapter; import java. awt. event. mouseEvent; import java. io. bufferedReader; import java. io. file; import java. io. fileReader; import java. io. IOException; import javax. swing. JButton; import javax. swing. JFileChooser; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JOptionPane; import javax. swing. JPanel; import javax. Swing. JTextField; import javax. swing. border. emptyBorder; @ SuppressWarnings ("serial") public class CountRows extends JFrame {private JPanel contentPane; private JTextField absolutePath; private int num; // private String path used to store the number of rows; /*** Launch the application. */public static void main (String [] args) {EventQueue. invokeLater (new Runnable () {public void run () {try {CountRows frame = new Count Rows (); frame. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}});}/*** Create the frame. */public CountRows () {setTitle ("\ u7EDF \ u8BA1 \ u4EE3 \ u7801 \ u884C \ u6570"); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,384,185); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5, 5); setContentPane (contentPane); contentPane. setLayout (null); AbsolutePath = new JTextField (); absolutePath. setBounds (67, 39,200, 31); contentPane. add (absolutePath); absolutePath. setColumns (10); JLabel lblSrc = new JLabel ("src \ u8DEF \ u5F84"); lblSrc. setFont (new Font ("", Font. PLAIN, 15); lblSrc. setBounds (10, 39, 64, 31); contentPane. add (lblSrc); JButton result = new JButton ("\ u7EDF \ u8BA1 \ u884C \ u6570"); result. addMouseListener (new MouseAdapter () {@ Overri De public void mouseClicked (MouseEvent e) {String path = absolutePath. getText (); File file = new File (path); try {nums (file);} catch (IOException e1) {e1.printStackTrace ();} JOptionPane. showMessageDialog (contentPane, "code:" + num + "row") ;}}); result. setFont (new Font ("", Font. PLAIN, 14); result. setBounds (48,100, 93, 37); contentPane. add (result); JButton exit = new JButton ("\ u9000 \ u51FA "); Exit. addMouseListener (new MouseAdapter () {@ Override public void mouseClicked (MouseEvent e) {System. exit (1) ;}}); exit. setFont (new Font ("", Font. PLAIN, 14); exit. setBounds (270,100, 93, 37); contentPane. add (exit); JButton view = new JButton ("\ u6D4F \ u89C8"); view. addMouseListener (new MouseAdapter () {@ Override public void mouseClicked (MouseEvent e) {JFileChooser jfc = new JFileChooser ("c: \ "); Jfc. setFileSelectionMode (JFileChooser. DIRECTORIES_ONLY); jfc. setDialogTitle ("Select the src folder for statistics"); int result = jfc. showOpenDialog (contentPane); if (result = JFileChooser. APPROVE_OPTION) {path = jfc. getSelectedFile (). getAbsolutePath (); absolutePath. setText (path) ;}}); view. setBounds (277, 36, 81, 37); contentPane. add (view);}/*** write a method to calculate the amount of code ** @ throws IOException */private void nums (Fi Le file) throws IOException {if (file. isDirectory () {File [] files = file. listFiles (); for (int I = 0; I <files. length; I ++) {File f = files [I]; nums (f) ;}} else {BufferedReader br = new BufferedReader (new FileReader (file )); while (br. readLine ()! = Null) {num ++;} br. close ();}}}

The above is the details of a small program that collects statistics on the amount of code. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.