Java code statistics Tool

Source: Internet
Author: User

 

The Code statistics tool is not very functional. You are welcome to give more comments.

Import java. awt. event. ActionEvent;

Import java. awt. event. ActionListener;

Import java. io. BufferedReader;

Import java. io. File;

Import java. io. FileNotFoundException;

Import java. io. FileReader;

Import java. io. IOException;

Import javax. swing. JFileChooser;

Import javax. swing. JFrame;

Import javax. swing. JMenu;

Import javax. swing. JMenuBar;

Import javax. swing. JMenuItem;

Import javax. swing. JScrollPane;

Import javax. swing. JTextArea;

Import javax. swing. UIManager;

 

Public class CodeCounterFrame extends JFrame

{

JMenuBar menuBar = new JMenuBar ();

JMenu fileMenu = new JMenu ("file ");

JMenuItem openMenuItem = new JMenuItem ("open ");

JTextArea txa = new JTextArea ();

JScrollPane jsp = new JScrollPane (txa );

String output = "";

Static long codeLines = 0;

Static long commentLines = 0;

Static long blankLines = 0;

Public CodeCounterFrame ()

{

This. setJMenuBar (menuBar );

MenuBar. add (fileMenu );

FileMenu. add (openMenuItem );

Txa. setEditable (false); // contents in txa cannot be edited.

Add (jsp );

// Internal anonymous listener

OpenMenuItem. addActionListener (new ActionListener ()

{

Public void actionreceivmed (ActionEvent e)

{

If (e. getSource () = openMenuItem)

{

// Click Open menu

// Pop-up file Selector

JFileChooser chooser = new JFileChooser ();

Chooser. showOpenDialog (null );

// Obtain the selected file

File selectedFile = chooser. getSelectedFile ();

BufferedReader br = null;

Boolean flag = false;

Try {

FileReader reader = new FileReader (selectedFile); // read the document content

Br = new BufferedReader (reader );

String line = "";

// 2 read the file

While (line = br. readLine ())! = Null)

{

Line = line. trim (); // remove the space before the Annotation

If (line. matches ("^ [] * $ "))

{

// Match empty rows

BlankLines ++;

}

Else if (line. startsWith ("//"))

{

CommentLines ++;

}

Else if (line. startsWith ("/*")&&! Line. endsWith ("*/"))

{

CommentLines ++;

Flag = true;

}

Else if (line. startsWith ("/*") & line. endsWith ("*/"))

{

CommentLines ++;

}

Else if (flag = true)

{

CommentLines ++;

If (line. endsWith ("*/"))

{

Flag = false;

}

}

Else

{

CodeLines ++;

}

}

 

// Save the result to output

Output + = ("code lines:" + codeLines + "\ n" + "comment lines:" + commentLines + "\ n"

+ "Number of blank lines:" + balancer lines + "\ n" + "Total number of lines:" + (codeLines + commentLines + balancer lines ));

// Display the statistical result in txa

Txa. setText (output );

}

Catch (FileNotFoundException ex)

{

Ex. printStackTrace ();

}

Catch (IOException ex)

{

Ex. printStackTrace ();

}

Finally

{

If (br! = Null)

{

Try {

Br. close ();

Br = null;

}

Catch (IOException ex)

{

Ex. printStackTrace ();

}

}

}

}

}

});

}

// Frame main program

Public static void main (String [] args)

{

Try {

UIManager. setLookAndFeel ("com. sun. java. swing. plaf. windows. WindowsLookAndFeel"); // windows interface style

} Catch (Exception e ){

E. printStackTrace ();

}

CodeCounterFrame frame = new CodeCounterFrame ();

Frame. setTitle ("small tool for code Statistics ");

Frame. setSize (300,200 );

Frame. setVisible (true );

Frame. setLocationRelativeTo (null); // The center of the form is displayed.

Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );

}

}

Run window:

 

Output result:

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.