去掉注释

2005-12-26 16:27:28 J2ME经验 Views(1402)

去掉注释对内存,大小都没有影响的,想想编译原理吧。
哎,做了半天无用功。

不过还是写了个java程序,小有收获。
汗一下,没怎么写过java小程序。

需要注意的地方:
main方法只能为static,所以要在main方法里面调用其他方法,需要先new一个该类,然后调用这个类的方法。

import java.io.*;
import java.util.Vector;

public class FileRead {

static String infname;// = "input.java";

static String outname;// = "output.java";

File fin = null;

File fout = null;

BufferedReader in = null;

BufferedWriter rf = null;

Vector vin = new Vector();

int state = 0;

public static void main(String[] args) {
infname = args[0];
outname = args[1];
FileRead fr = new FileRead();

try {
fr.FRInit();

fr.FRRead();

fr.FRWrite();

fr.FRClose();

} catch (FileNotFoundException e1) {

} catch (IOException e) {

}
}

public void FRInit() throws IOException {
fin = new File(infname);
fout = new File(outname);

in = new BufferedReader(new FileReader(fin));
rf = new BufferedWriter(new FileWriter(fout));

}

public void FRRead() throws IOException {
String line = in.readLine();
while (line != null) {
FRReadState(line);
line = in.readLine();
}
}

private void FRReadState(String line) {
int linestart;
switch (state) {
case 0:
FRReadStateNormal(line);
break;
case 1:
FRReadStateStar(line);
break;
default:
break;
}

}

private void FRReadStateNormal(String line){
int linestart = line.indexOf("//");
if (linestart != -1){
String newline = line.substring(0,linestart);
vin.addElement(newline);
}else if(line.indexOf("/*") != -1){
state = 1;
}else{
vin.addElement(line);
}
}

private void FRReadStateStar(String line){
if (line.indexOf("*/") != -1){
state = 0;
}
}

public void FRWrite() throws IOException {
while (!vin.isEmpty()) {
String rline = (String) vin.firstElement();
rf.write(rline);
rf.newLine();
vin.removeElementAt(0);
}
}

public void FRClose() throws IOException {
in.close();
rf.close();
}
}

觉得自己的代码风格稍有提高,鼓励一下。

Comments

Leave a Comment

Name
Content
Verification Type the characters you see in the picture below