- 将形如/**/已经//的注释都去掉,代码如下:
- 这是别人写的,只是拿过来参考下
package removeNotes;
/**
* Created by 12083 on 2016/9/7.
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveNotes {
public static void main(String[] args) {
String test = ReadFileToString("E:/main.java");
System.out.println(removeCommentsWithQuoteAndDoubleEscape(test));
}
/**
* 处理双引号和双斜杠注释
*/
public static String removeCommentsWithQuoteAndDoubleEscape(String code) {
StringBuilder sb = new StringBuilder();
int cnt = 0;
boolean quoteFlag = false;
for (int i = 0; i < code.length(); i++) {
if (!quoteFlag) {
if (code.charAt(i) == '\"') {
sb.append(code.charAt(i));
quoteFlag = true;
continue;
}
else if (i + 1 < code.length() && code.charAt(i) == '/' && code.charAt(i + 1) == '/') {
while (code.charAt(i) != '\n') {
i++;
}
continue;
}
else {
if (cnt == 0) {
if (i + 1 < code.length() && code.charAt(i) == '/' && code.charAt(i + 1) == '*') {
cnt++;
i++;
continue;
}
} else {
if (i + 1 < code.length() && code.charAt(i) == '*' && code.charAt(i + 1) == '/') {
cnt--;
i++;
continue;
}
if (i + 1 < code.length() && code.charAt(i) == '/' && code.charAt(i + 1) == '*') {
cnt++;
i++;
continue;
}
}
if (cnt == 0) {
sb.append(code.charAt(i));
continue;
}
}
}
else {
if (code.charAt(i) == '\"' && code.charAt(i - 1) != '\\') {
sb.append(code.charAt(i));
quoteFlag = false;
}
else {
sb.append(code.charAt(i));
}
}
}
return sb.toString();
}
/**
* 从一个文件读入到String
*
* @param FilePath
* @return
*/
public static String ReadFileToString(String FilePath) {
FileInputStream fis = null;
BufferedReader br = null;
try {
fis = new FileInputStream(FilePath);
br = new BufferedReader(new InputStreamReader(fis, "utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer sb = new StringBuffer();
String temp = null;
try {
while ((temp = br.readLine()) != null) {
sb.append(temp + '\n');
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
String removeComments(String curr) {
StringBuilder builder = new StringBuilder();
boolean isQuote = false;
for (int i = 0; i < curr.length(); ++i) {
if (!isQuote) {
//now meet the close quote
if (curr.charAt(i) == '/' && i + 1 < curr.length() && curr.charAt(i + 1) == '/') {
while (i < curr.length() && curr.charAt(i) != '\n')
i++;
continue;
} else if (curr.charAt(i) == '/' && i + 1 < curr.length() && curr.charAt(i + 1) == '*') {
i += 2;
while (i < curr.length() && curr.charAt(i) != '*' && i + 1 < curr.length() && curr.charAt(i + 1) != '/')
i++;
i++;
continue;
} else if (curr.charAt(i) == '"') {
builder.append(curr.charAt(i));
isQuote = true;
continue;
}
builder.append(curr.charAt(i));
} else {
while (i < curr.length() && curr.charAt(i) != '"')
builder.append(curr.charAt(i++));
builder.append('"');
isQuote = false;
}
}
return builder.toString();
}
public static void main(String[] args) {
print p = new print();
String s = "int candy(int[] arr) { " +
"int[] candy = new int[arr.length]; " +
"for (int i = 0; i < candy.length; ++i)" +
" candy[i] = 1;" +
"for (int i = 1; i < arr.length; ++i) { " +
" if (arr[i] > arr[i - 1]) " +
" candy[i] = candy[i - 1] + 1; " +
"}" +
"for (int i = candy.length - 2; i >= 0; --i) {" +
"if (arr[i] > arr[i + 1])" +
"candy[i] = Math.max(candy[i - 1], candy[i + 1]);" +
"}" +
"int total = 0;" +
"for (int i = 0; i < candy.length; ++i)" +
"total += candy[i];" + "String s = \"/*wangcheng*/\";" +
"return total;" +
"}";
String str = p.removeComments(s);
System.out.println(str);
System.out.println();
}
public String readFile(String fileName) throws FileNotFoundException {
File file = new File(fileName);
if (!file.exists()) {
throw new FileNotFoundException("The file " + fileName + " not found!");
}
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuilder sb = new StringBuilder();
String tmp;
try {
while ((tmp = bf.readLine()) != null) {
sb.append(tmp);
sb.append("\n");
}
} catch (IOException ex) {
ex.printStackTrace();
}
return sb.toString();
}