请问用java代码如何读取java的注释? 如题,用java代码来读取java注释,而不用第三方软件。可以的话,希望可以把代码粘贴出来,谢谢~! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java注释无非就包括:"//","/*……*/","/***/"三种,可以根据这几种情况分步进行! 可以读,标注,还有一些框架也支持,你看看API就是doc啊,人家怎么读出来了,对吧 如何能轻松的写,那javadoc也没啥必要了! 看了下javadoc的源码,没看到一点能用的东西,哎! 无非就是读文件,再分析当前行的信息,贴下之前写的关于统计Java源文件的代码行,但是只是统计代码和注释的行数,没有分析内容了,其实也不用分析,直接输出就好了。import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class CountCodes extends JFrame{ private long codeLines = 0; private long commentLines = 0; private long blankLines = 0; private File fileName; private JLabel pathLabel; private JTextField pathField; private JButton openButton; private JPanel panelNorth, panelSouth; private ArrayList<File> list = new ArrayList<File> (); private String[] tableHeader = {"File Name", "Code Lines", "Comment Lines", "Blank Lines"}; private Vector<String> vectorHeader = new Vector<String> (); private Vector<Vector> values = new Vector<Vector> (); private JTable table; private DefaultTableModel tableModel; private JScrollPane scrollPane; public CountCodes(){ super("Count Codes"); pathLabel = new JLabel("File Path: "); pathField = new JTextField(100); pathField.setEditable(false); openButton = new JButton("Open"); openButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ openFile(); if (fileName == null) return; analysisFileOrDir(fileName); for(File file : list){ analyze(file); } } }); panelNorth = new JPanel(); panelNorth.setLayout(new BorderLayout()); panelNorth.add(pathLabel, BorderLayout.WEST); panelNorth.add(pathField, BorderLayout.CENTER); panelNorth.add(openButton, BorderLayout.EAST); for (int i=0; i<tableHeader.length; i++) vectorHeader.add(tableHeader[i]); tableModel = new DefaultTableModel(values, vectorHeader); table = new JTable(tableModel); scrollPane = new JScrollPane(table); panelSouth = new JPanel(); panelSouth.setLayout(new BorderLayout()); panelSouth.add(scrollPane, BorderLayout.CENTER); setLayout(new BorderLayout()); add(panelNorth, BorderLayout.NORTH); add(panelSouth, BorderLayout.CENTER); setSize(450, 400); setVisible(true); } public static void main(String args[]){ CountCodes countCodes = new CountCodes(); countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void openFile(){ JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.CANCEL_OPTION) return; fileName = fileChooser.getSelectedFile(); if (fileName == null || fileName.getName().equals("")) JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE); pathField.setText(fileName.toString()); } private void zeroVariables(){ this.codeLines = 0; this.commentLines = 0; this.blankLines = 0; } private void analysisFileOrDir(File fileName){ if(fileName.isFile()){ list.add(fileName); return; } else{ File[] fileArray = fileName.listFiles(); for(File inArray : fileArray){ if(inArray.isFile() && inArray.getName().matches(".*\\.java$")){ list.add(inArray); } else if(inArray.isDirectory()){ analysisFileOrDir(inArray); } } } } private void analyze(File fileName){ Vector<String> value = new Vector<String> (); String[] strArray = fileName.toString().split("\\\\"); value.add(strArray[strArray.length-1]); BufferedReader br = null; boolean comment = false; try { br = new BufferedReader(new FileReader(fileName)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("^[\\s&&[^\\n]]*$")) { blankLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { commentLines++; comment = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { commentLines++; } else if (true == comment) { commentLines++; if (line.endsWith("*/")) { comment = false; } } else if (line.startsWith("//")) { commentLines++; } else { codeLines++; } } value.add(String.valueOf(codeLines)); value.add(String.valueOf(commentLines)); value.add(String.valueOf(blankLines)); values.add(value); this.table.revalidate(); zeroVariables(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } } 不是有自己带的javadoc命令可以生成文档吗? 你的意思是要自己写一个? 对,7楼说的对啊,有java的javadoc命令 利用中饭时间简单的写了下解析"//"与"/*..*/",剩下的同理基本能分析出注释,但是遇到代码中如果本来就应用了"//","/*"和"*/"应该会出问题,适当修改就可以了下午困,得睡会import java.io.*;public class CC{ public static BufferedReader br=null; public static File file = new File("d:\\test\\aaa.java"); public static void main(String[] args){ try{ br = new BufferedReader(new FileReader(file)); StringBuffer sb=new StringBuffer(); boolean flag=false; String temp = br.readLine(); //System.out.println(temp); while(temp!=null){ if(temp.indexOf("//")!=-1&&!flag){ String x = temp.substring(temp.indexOf("//")+2); System.out.println("//注释:"+x); temp = br.readLine(); }else if(temp.indexOf("/*")!=-1||temp.indexOf("*/")!=-1||flag){ if(temp.indexOf("/*")!=-1&&temp.indexOf("*/")!=-1){ String x = temp.substring(temp.indexOf("/*")+2,temp.indexOf("*/")-1); System.out.println(x); temp = br.readLine(); }else if(temp.indexOf("/*")!=-1){ //System.out.println(temp.substring(temp.indexOf("/*")+2)); sb=sb.append(temp.substring(temp.indexOf("/*")+2)); flag=true; temp = br.readLine(); }else if(flag&&temp.indexOf("*/")==-1){ sb.append(temp); temp = br.readLine(); }else{ sb=sb.append(temp.substring(0,temp.indexOf("*/"))); System.out.println("/**/注释:"+sb); flag=false; sb.delete(0,sb.length()-1); temp=br.readLine(); } }else{ temp = br.readLine(); } } }catch(Exception e){ e.printStackTrace(); }finally{ try{ br.close(); }catch(Exception e){ e.printStackTrace(); } } }} Swing弹出窗口后如何使当前窗口(包括windows和java生成的)不丢失焦点 今天刚装了GUI生成器插件,在哪可以学习到运用。 获取return返回值的问题 用 java JNI 调用一个DLL文件,实现禁止终止进程 weblogic8登陆密码 原来是这样,值得学习 求用java写的viterbi算法源码? 一个选择题,请求答案!! 小弟刚刚接触java,就遇到一个严重的问题,大家帮帮忙了。 关于接口的作用 初学者问题while 如何计算圆的坐标
可以根据这几种情况分步进行!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*; import javax.swing.*;
import javax.swing.table.DefaultTableModel; public class CountCodes extends JFrame{
private long codeLines = 0;
private long commentLines = 0;
private long blankLines = 0;
private File fileName;
private JLabel pathLabel;
private JTextField pathField;
private JButton openButton;
private JPanel panelNorth, panelSouth; private ArrayList<File> list = new ArrayList<File> ();
private String[] tableHeader = {"File Name", "Code Lines", "Comment Lines", "Blank Lines"};
private Vector<String> vectorHeader = new Vector<String> ();
private Vector<Vector> values = new Vector<Vector> ();
private JTable table;
private DefaultTableModel tableModel;
private JScrollPane scrollPane;
public CountCodes(){
super("Count Codes");
pathLabel = new JLabel("File Path: ");
pathField = new JTextField(100);
pathField.setEditable(false);
openButton = new JButton("Open");
openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
openFile();
if (fileName == null)
return;
analysisFileOrDir(fileName);
for(File file : list){
analyze(file);
}
}
});
panelNorth = new JPanel();
panelNorth.setLayout(new BorderLayout());
panelNorth.add(pathLabel, BorderLayout.WEST);
panelNorth.add(pathField, BorderLayout.CENTER);
panelNorth.add(openButton, BorderLayout.EAST);
for (int i=0; i<tableHeader.length; i++)
vectorHeader.add(tableHeader[i]);
tableModel = new DefaultTableModel(values, vectorHeader); table = new JTable(tableModel);
scrollPane = new JScrollPane(table);
panelSouth = new JPanel();
panelSouth.setLayout(new BorderLayout());
panelSouth.add(scrollPane, BorderLayout.CENTER);
setLayout(new BorderLayout());
add(panelNorth, BorderLayout.NORTH);
add(panelSouth, BorderLayout.CENTER);
setSize(450, 400);
setVisible(true);
}
public static void main(String args[]){
CountCodes countCodes = new CountCodes();
countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void openFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION)
return;
fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
pathField.setText(fileName.toString());
}
private void zeroVariables(){
this.codeLines = 0;
this.commentLines = 0;
this.blankLines = 0;
}
private void analysisFileOrDir(File fileName){
if(fileName.isFile()){
list.add(fileName);
return;
}
else{
File[] fileArray = fileName.listFiles();
for(File inArray : fileArray){
if(inArray.isFile() && inArray.getName().matches(".*\\.java$")){
list.add(inArray);
}
else if(inArray.isDirectory()){
analysisFileOrDir(inArray);
}
}
}
}
private void analyze(File fileName){
Vector<String> value = new Vector<String> ();
String[] strArray = fileName.toString().split("\\\\");
value.add(strArray[strArray.length-1]);
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(fileName));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[\\s&&[^\\n]]*$")) {
blankLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines++;
comment = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLines++;
} else if (true == comment) {
commentLines++;
if (line.endsWith("*/")) {
comment = false;
}
} else if (line.startsWith("//")) {
commentLines++;
} else {
codeLines++;
}
}
value.add(String.valueOf(codeLines));
value.add(String.valueOf(commentLines));
value.add(String.valueOf(blankLines));
values.add(value);
this.table.revalidate();
zeroVariables();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
基本能分析出注释,但是遇到代码中如果本来就应用了"//","/*"和"*/"应该会出问题,适当修改就可以了
下午困,得睡会
import java.io.*;public class CC
{
public static BufferedReader br=null;
public static File file = new File("d:\\test\\aaa.java");
public static void main(String[] args){
try{
br = new BufferedReader(new FileReader(file));
StringBuffer sb=new StringBuffer();
boolean flag=false;
String temp = br.readLine();
//System.out.println(temp);
while(temp!=null){
if(temp.indexOf("//")!=-1&&!flag){
String x = temp.substring(temp.indexOf("//")+2);
System.out.println("//注释:"+x);
temp = br.readLine();
}else if(temp.indexOf("/*")!=-1||temp.indexOf("*/")!=-1||flag){
if(temp.indexOf("/*")!=-1&&temp.indexOf("*/")!=-1){
String x = temp.substring(temp.indexOf("/*")+2,temp.indexOf("*/")-1);
System.out.println(x);
temp = br.readLine();
}else if(temp.indexOf("/*")!=-1){
//System.out.println(temp.substring(temp.indexOf("/*")+2));
sb=sb.append(temp.substring(temp.indexOf("/*")+2)); flag=true;
temp = br.readLine();
}else if(flag&&temp.indexOf("*/")==-1){
sb.append(temp);
temp = br.readLine();
}else{
sb=sb.append(temp.substring(0,temp.indexOf("*/")));
System.out.println("/**/注释:"+sb);
flag=false;
sb.delete(0,sb.length()-1);
temp=br.readLine();
}
}else{
temp = br.readLine();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}