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);
}
}
}