62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
File file = new File("D:/Green/jdk1.5src/java/io/File.java");
String str = readFile2String(file);
str = str.replaceAll("(?s)/\\*.*?\\*/", "");
System.out.println(str);
}
private static String readFile2String(File file) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line = System.getProperty("line.separator");
try {
br = new BufferedReader(new FileReader(file));
String str = null;
while((str = br.readLine()) != null) {
sb.append(str).append(line);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
}
// Test File
package color;
/**
* My firat Java program: Hello World
* @author bao110908
*/
public class Test{
/*
main method
method of automatic startup
*/
/* abnormal comment 1 */
/** abnormal comment 2 **/
// normal comment
public static void main(String[] args) {
System.out.println("Hello World"); // print 'Hello World'
System.out.println("Hello World"); /* abnormal comment 3 */
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Comment comment = new Comment();
int count = comment.countCommentLine("abc.java");
System.out.println("总共的注释行数:" + count);
System.out.println();
List<String> codes = comment.removeComment("abc.java");
System.out.println("没有注释的源代码");
for (String str : codes) {
System.out.println(str);
}
}
}
class Comment {
/**
* 计算某个 Java 文件中注释的行数
*/
public int countCommentLine(String filename) {
if (filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
int count = 0;
List<String> list = Util.readFile(filename);
boolean isMultiLineComment = false;
for (String str : list) {
// 当为多行注释时,直接将计算注释行
if (isMultiLineComment) {
count++;
// 直到碰到 */ 为止,当然 */ 这一行也算注释
if (str.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
}
// 为 // 直接计数
if (str.indexOf("//") >= 0) {
count++;
}
// 当碰到 /* 时,开始计数
if (str.indexOf("/*") >= 0) {
count++;
// 并且在该行中没 */ 时,可以看作是多行注释的开始
if (str.indexOf("*/") < 0) {
isMultiLineComment = true;
}
}
}
return count;
}
/**
* 除去文件中的注释,与 countCommentLine 方法类似
*/
public List<String> removeComment(String filename) {
if (filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
List<String> list = Util.readFile(filename);
List<String> codes = new ArrayList<String>();
boolean isMultiLineComment = false;
for (int i = 0, k = list.size(); i < k; i++) {
String code = list.get(i);
if (isMultiLineComment) {
if (code.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
continue;
}
int index = code.indexOf("/*");
if (index >= 0) {
String s = code.substring(0, index);
if (s.trim().length() > 0) {
codes.add(s);
}
if (code.indexOf("*/") < 0) {
isMultiLineComment = true;
}
continue;
}
index = code.indexOf("//");
if (index >= 0) {
String s = code.substring(0, index);
if (s.trim().length() > 0) {
codes.add(s);
}
continue;
}
codes.add(code);
}
return codes;
}
}
class IllegalJavaFile extends RuntimeException {
private static final long serialVersionUID = 1L;
public IllegalJavaFile(String filename) {
super("\"" + filename + "\", the file is not a java file.");
}
}
class Util {
public static List<String> readFile(String filename) {
List<String> list = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
String str = "";
while ((str = br.readLine()) != null) {
// 忽略空行
if (str.trim().length() > 0) {
list.add(str);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
public static void writeFile(List<String> codes, String filename) {
BufferedWriter br = null;
try {
br = new BufferedWriter(new FileWriter(filename));
String line = System.getProperty("line.separator");
for (int i = 0, k = codes.size(); i < k; i++) {
br.write(codes.get(i));
br.write(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
void fileCopy1(File src, File dest) {
BufferedInputStream bis;
BufferedOutputStream bos;
Pattern p = Pattern.compile("(\\/\\**\\s*\\n*([.&&[^/]])*?\\**\\/)");
byte[] b = new byte[1024];
try {
bis = new BufferedInputStream(new FileInputStream(src));
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = bis.read(b)) != -1) {
sb.append(new String(b, 0, len));
}
bos = new BufferedOutputStream(new FileOutputStream(dest));
String str = sb.toString();
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println("find");
m.replaceAll("");
}
bos.write(str.getBytes());
} catch (FileNotFoundException fnfex) {
System.out.println(fnfex.getMessage());
} catch (IOException ioex) {
System.out.println(ioex.getMessage());
} catch(Exception ex) {
System.out.println("message ======" + ex.getMessage());
}
}