How to count LOC of a program Using Java

Here i am using file method to get the number of lines. First you have to create file and put your programs inside that file. here i created a file program1. so in my output you can see the prgrams written in program1 file. By using this program that i explained below you can easily call the created files and easily you will get the LOC.

import java.io.*;
import java.util.*;
public class Read {
public static void main(String[] args) throws IOException{
Scanner input= new Scanner(System.in);
System.out.print("Please enter the file name to Open : ");
String file_name = input.nextLine();
FileInputStream fstream = new FileInputStream(file_name);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br1 = new BufferedReader(new InputStreamReader(in));
String strLine;
int count = 0;
int count2 = 0;
//Read File Line By Line
while ((strLine = br1.readLine()) != null) {
    if (strLine.trim().length() != 0){
        System.out.println(strLine);   
        count++;
        }else{
        count2++;
        }
// Print the content on the console
//System.out.println (strLine);
}
//Close the input stream

System.out.println("number of lines:" + count);
System.out.println("number of Spaces:" + count2);

in.close();
System.out.println("File read successfully.");
}
}

Output:  


Please enter the file name to Open : program1
/*
// this is my sample program to count LOC,
import java.io.*;
public class Test{
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the file name to create : ");
String file_name = in.readLine();
File file = new File(file_name);
boolean exist = file.createNewFile();
if (!exist){
System.out.println("File already exists.");
System.exit(0);}
else{
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
out.write(in.readLine());
out.close();
System.out.println("File created successfully.");
}}}

*/


number of lines:18
number of Spaces:17
File read successfully.








4 comments :

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

    ReplyDelete
  2. Thanks for sharing this great article. It made me understand few things about this concept which I never knew before. Keep posting such great articles so that I gain from it. Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

    ReplyDelete

Copyright © Rough Record. Designed by OddThemes