Welcome, %1$s. Please login or register.

August 18, 2017, 02:45:50 AM
: 1
: Java File to remove comments from another java file  ( 10263 )
« : December 08, 2010, 12:34:57 PM From A»



Java File to remove comments from another java file


Sample Program to remove comments from a java file.

Code: [Select]
/*
 * Author: Karanth Srihari
 * Date: 08/Dec/2010
 * Java File to remove comments from another java file
 */
import java.io.*;

public class RemoveComments {
public void readAndPrintFile(String fileName) {
int ch;
boolean tokenCheck = false;
boolean slashCommentFound = false;
boolean starCommentFound = false;
boolean firstSlashFound = false;
boolean firstStarFound = false;
boolean closingStarFound = false;
boolean startDoubleQuoteFound = false;
int lastChar;

try {

BufferedReader reader = new BufferedReader(new FileReader(fileName));

while ((ch = reader.read()) != -1) {

lastChar = ch;

if (ch == '\"') {
if (startDoubleQuoteFound == false) {
startDoubleQuoteFound = true;
} else if (startDoubleQuoteFound == true) {
startDoubleQuoteFound = false;
}
}

if (startDoubleQuoteFound
&& (starCommentFound == true || slashCommentFound == true)) {
continue;
}
if (ch == '/') {
if (starCommentFound == true && closingStarFound == false) {
continue;
}
if (closingStarFound && starCommentFound == true) {
starCommentFound = false;
closingStarFound = false;
firstStarFound = false;
continue;
} else if (firstSlashFound && slashCommentFound == false
&& starCommentFound == false) {
slashCommentFound = true;
firstSlashFound = false;
continue;
} else if (slashCommentFound == false
&& starCommentFound == false
&& startDoubleQuoteFound == false) {
firstSlashFound = true;
continue;
}
}
if (ch == '*') {
if (starCommentFound) {
closingStarFound = true;
continue;
}
if (firstSlashFound && starCommentFound == false) {
starCommentFound = true;
firstSlashFound = false;
continue;
} else if (firstStarFound == false
&& starCommentFound == true) {
firstStarFound = true;
continue;
}
}
if (ch == '\n') {
if (slashCommentFound) {
slashCommentFound = false;
firstStarFound = false;
firstSlashFound = false;
starCommentFound = false;
System.out.print((char) ch);
continue;
}
}

if (starCommentFound == true && closingStarFound == false) {
continue;
}

if (ch != '/' && ch != '*') {
if (closingStarFound) {
System.out.print((char) lastChar);
}

closingStarFound = false;
firstSlashFound = false;
firstStarFound = false;
closingStarFound = false;

}

if (slashCommentFound == false && starCommentFound == false) {
System.out.print((char) ch);
}
}

reader.close();

} catch (FileNotFoundException ex) {
System.out.println(fileName + " not found");
} catch (Exception ex) {
System.out.println("Error reading file " + fileName);
ex.printStackTrace();
}
}

public static void main(String[] args) {
RemoveComments reader = new RemoveComments();
reader.readAndPrintFile("D://Callback.java");
}
}

 
Liked It? Share it!

              


: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer