Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need a java program that uses buffers or streams to read a java file and remove all the comment lines from the file and store it in another file.I hv a sample code but i need other methods to do the same

import java.io.*;
public class remove{
public static void main(String args[]) { 
try{
InputStream inputStream = new FileInputStream("C:/Users/Administrator.E2INFO/Desktop/Nambi/GuestRoomService.java");
Reader reader= new InputStreamReader(inputStream);
OutputStream outputStream = new FileOutputStream("C:\\Users\\Administrator.E2INFO\\Desktop\\Nambi\\output.java");
Writer writer= new OutputStreamWriter(outputStream);
boolean inBlockComment = false;  
boolean inSlashSlashComment = false;  
    int char1 = reader.read();  
    if (char1 != -1) {  
        int char2;  
        while (char1 != -1) {  
            if ((char2 = reader.read()) == -1) {  
                writer.write(char1);  
                break;  
            }  
            if (char1 == '/' && char2 == '*') {  
                inBlockComment = true;  
                char1 = reader.read();  
                continue;  
            } else if (char1 == '*' && char2 == '/') {  
                inBlockComment = false;  
                char1 = reader.read();  
                continue;  
            } else if (char1 == '/' && char2 == '/' && !inBlockComment) {  
                inSlashSlashComment = true;  
                char1 = reader.read();  
                continue;  
            }  
            if (inBlockComment) {  
                char1 = char2;  
                continue;  
            }  
            if (inSlashSlashComment) {  
                if (char2 == '\n') {  
                    inSlashSlashComment = false;  
                    writer.write(char2);  
                    char1 = reader.read();  
                    continue;  
                } else if (char1 == '\n') {  
                    inSlashSlashComment = false;  
                    writer.write(char1);  
                    char1 = char2;  
                    continue;  
                } else {  
                    char1 = reader.read();  
                    continue;  
                }  
            }  
            writer.write(char1);  
            char1 = char2;  
        }  
        writer.flush();  
        writer.close();  
    }  
}
catch(IOException e)
{
System.out.println(e);
}     
}
}
share|improve this question

put on hold as too broad by Kon, MadProgrammer, rishi, Brad Larson 22 hours ago

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
Sure, how much are you paying for somebody to do your homework for you? I assume this because you're put no effort in. –  Kon 22 hours ago
2  
This question appears to be off-topic because it is ask for code to a problem and not asking a question to solve an existing issue –  MadProgrammer 22 hours ago
1  
I need more sleep, money, time and a helicopter while we're putting out wish lists –  MadProgrammer 22 hours ago
    
actually i did one for myself tis one is for my friend.. –  VenkatBlue 22 hours ago
1  
If you did one for yourself, then you would have a copy to give to your friend, you definitely wouldn't need to ask for another. Its not like source code can only be executed once and then its useless...lol Anyway....this might help ya ostermiller.org/findcomment.html –  KyleK 22 hours ago
show 5 more comments

Browse other questions tagged or ask your own question.