[java code]
if (child.getName().matches(".*\\.java$")) {
String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
parse(child);
fname = child.getName();// 文件名
flength = String.valueOf(child.length());// 文件大小
nocode = String.valueOf(normalLines);
cocode = String.valueOf(commentLines);
whcode = String.valueOf(whiteLines);
codetotal = String.valueOf(normalLines + commentLines
+ whiteLines);
listFiles(fname, flength, nocode, cocode, whcode,
codetotal);
} else if (child.getName().matches(".*\\.txt$")) {
String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
parse(child);
fname = child.getName();// 文件名
flength = String.valueOf(child.length());// 文件大小
nocode = String.valueOf(normalLines);
cocode = String.valueOf(commentLines);
whcode = String.valueOf(whiteLines);
codetotal = String.valueOf(normalLines + commentLines
+ whiteLines);
listFiles(fname, flength, nocode, cocode, whcode,
codetotal);
}
[/java code]

解决方案 »

  1.   

    [code=Java]
    if (child.getName().matches(".*\\.java$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    } else if (child.getName().matches(".*\\.txt$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    }
    [/code] 
      

  2.   

    if (child.getName().matches(".*\\.java$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    } else if (child.getName().matches(".*\\.txt$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    }
      

  3.   

    你点一张纸上面有一个#的按钮,选java,会自动出来你要的东西.你不要用我给你的那个啊,那的[我是用全角打的,应该改为半角的.
      

  4.   

    for (File child : files) {
    if (child.getName().matches(".*\\.java$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    } else if (child.getName().matches(".*\\.txt$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    }
    }
    不好意思,总算成功了.
    我的问题,为什么我在分支前加的初始化设空String字符串不起作用?
      

  5.   

    有啊.....我把全程序贴上来吧...package com.home;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FileDialog;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Vector;import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.text.html.ListView;public class CodeCount extends JFrame {
    JLabel jl_version, jl_normalLines, jl_commentLines, jl_whiteLines,
    jl_ntext, jl_ctext, jl_wtext;// 说明标签,有效代码,注释代码,空行
    JFileChooser chooser;// 选择文件控件
    JButton jbtn_openfile, jbtn_begin, jbtn_clear, jbtn_list;// 选择文件按钮,开始计算按钮,清空按钮,文件清单
    JScrollPane jsc_list;// 文件细则
    JTable view;// 文件列表
    DefaultTableModel dtm;
    File file;
    File[] files;
    Object[][] fileinfo;// 文件信息
    String[] tablehead = { "文件名", "大小(byte)", "有效代码", "注释", "空行", "总计" };;
    static long normalLines = 0;
    static long commentLines = 0;
    static long whiteLines = 0;
    static String filename; public CodeCount() {
    this.setTitle("代码统计小软件");
    this.setBounds(250, 250, 440, 500);
    this.setLayout(null);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public static void main(String[] args) {
    new CodeCount().indexMain();
    } public void indexMain() {
    jl_version = new JLabel();
    jl_version.setText("统计代码的一个小程序----by Reeves ");
    jl_version.setBounds(220, 430, 300, 30);
    this.add(jl_version); // 选择文件按钮
    jbtn_openfile = new JButton("文件");
    jbtn_openfile.setBounds(50, 20, 80, 25);
    jbtn_openfile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    openFiles();
    }
    });
    this.add(jbtn_openfile);
    // 开始按钮
    jbtn_begin = new JButton("计算");
    jbtn_begin.setBounds(jbtn_openfile.getX(), jbtn_openfile.getY() + 30,
    80, 25);
    this.add(jbtn_begin);
    jbtn_begin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    for (File child : files) {
    if (child.getName().matches(".*\\.java$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    } else if (child.getName().matches(".*\\.txt$")) {
    String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
    parse(child);
    fname = child.getName();// 文件名
    flength = String.valueOf(child.length());// 文件大小
    nocode = String.valueOf(normalLines);
    cocode = String.valueOf(commentLines);
    whcode = String.valueOf(whiteLines);
    codetotal = String.valueOf(normalLines + commentLines
    + whiteLines);
    listFiles(fname, flength, nocode, cocode, whcode,
    codetotal);
    }
    }
    long a = normalLines;
    long b = commentLines;
    long c = whiteLines;
    result(a, b, c);
    }
    }); // 值标签
    jl_normalLines = new JLabel("有效代码:");
    jl_commentLines = new JLabel("注         释:");
    jl_whiteLines = new JLabel("空         行:");
    jl_normalLines.setBounds(jbtn_openfile.getX() + 120, jbtn_openfile
    .getY() - 5, 120, 30);
    jl_commentLines.setBounds(jbtn_openfile.getX() + 120, jbtn_openfile
    .getY() - 5 + 50, 120, 30);
    jl_whiteLines.setBounds(jbtn_openfile.getX() + 120, jbtn_openfile
    .getY() - 5 + 90, 120, 30); jl_ntext = new JLabel();
    jl_ctext = new JLabel();
    jl_wtext = new JLabel();
    jl_ntext.setBounds(jbtn_openfile.getX() + 205,
    jbtn_openfile.getY() - 5, 80, 30);
    jl_ctext.setBounds(jbtn_openfile.getX() + 205,
    jbtn_openfile.getY() - 5 + 50, 80, 30);
    jl_wtext.setBounds(jbtn_openfile.getX() + 205,
    jbtn_openfile.getY() - 5 + 90, 80, 30);
    this.add(jl_ntext);
    this.add(jl_ctext);
    this.add(jl_wtext);
    // 清空按钮
    jbtn_clear = new JButton("清空");
    jbtn_clear.setBounds(jbtn_openfile.getX(), jbtn_openfile.getY() + 60,
    80, 25);
    jbtn_clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    jl_ntext.setText(null);
    jl_ctext.setText(null);
    jl_wtext.setText(null);
    normalLines = 0;
    commentLines = 0;
    whiteLines = 0;
    }
    });
    this.add(jbtn_clear);
    this.add(jl_normalLines);
    this.add(jl_commentLines);
    this.add(jl_whiteLines); // 详细按钮
    jbtn_list = new JButton("详细");
    jbtn_list.setBounds(jbtn_openfile.getX(), jbtn_openfile.getY() + 90,
    80, 25);
    this.add(jbtn_list);
    // 文件清单 dtm = new DefaultTableModel(fileinfo, tablehead);
    view = new JTable(dtm);
    view.setAutoCreateRowSorter(true);
    jsc_list = new JScrollPane(view);
    jsc_list.setBounds(jbtn_openfile.getX() - 20,
    jbtn_openfile.getY() + 130, 380, 280);
    this.add(jsc_list);
    this.setVisible(true);
    } public void result(long a, long b, long c) {
    jl_ntext.setText(String.valueOf(a) + "行");
    jl_ctext.setText(String.valueOf(b) + "行");
    jl_wtext.setText(String.valueOf(c) + "行");
    } // 文件详细清单
    public void listFiles(String n1, String n2, String n3, String n4,
    String n5, String n6) {
    Vector<String> v = new Vector<String>();
    v.add(n1);
    v.add(n2);
    v.add(n3);
    v.add(n4);
    v.add(n5);
    v.add(n6);
    dtm.addRow(v);
    } // 选择文件对话框
    public void openFiles() {
    chooser = new JFileChooser(
    JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY);
    chooser.setDialogTitle("选择文件"); // 过滤文件
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "TXT&JAVA", "txt", "java");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(true);
    chooser.showOpenDialog(this);
    files = chooser.getSelectedFiles();
    } // 业务逻辑
    private static void parse(File f) {
    BufferedReader br = null;
    boolean comment = false;
    try {
    br = new BufferedReader(new FileReader(f));
    String line = "";
    while ((line = br.readLine()) != null) {
    line = line.trim();
    if (line.matches("^[\\s&&[^\\n]]*$")) {
    whiteLines++;
    } 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 {
    normalLines++;
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (br != null) {
    try {
    br.close();
    br = null;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
      

  6.   

            for (File child : files) {
    你在这里要对这几个变量清零normalLines  commentLines   whiteLines 
      

  7.   

    for (File child : files) { 
    //加上下面这三条语句
       normalLines=0;
       commentLines=0;
       whiteLines =0;
      

  8.   

    你没有试呀,加在for循环中,不用说你就10个文件,你的10万个也管用的
      

  9.   

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FileDialog;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Vector;import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.text.html.ListView;public class CodeCount extends JFrame {
        JLabel jl_version, jl_normalLines, jl_commentLines, jl_whiteLines,
                jl_ntext, jl_ctext, jl_wtext;// 说明标签,有效代码,注释代码,空行
        JFileChooser chooser;// 选择文件控件
        JButton jbtn_openfile, jbtn_begin, jbtn_clear, jbtn_list;// 选择文件按钮,开始计算按钮,清空按钮,文件清单
        JScrollPane jsc_list;// 文件细则
        JTable view;// 文件列表
        DefaultTableModel dtm;
        File file;
        File[] files;
        Object[][] fileinfo;// 文件信息
        String[] tablehead = { "文件名", "大小(byte)", "有效代码", "注释", "空行", "总计" };;
        static long normalLines = 0;
        static long commentLines = 0;
        static long whiteLines = 0;       
        static long tatalNormal=0;          //********
        static long tatalComment=0;         //********
        static long tatalWhite=0;           //********
        static String filename;    public CodeCount() {
            this.setTitle("代码统计小软件");
            this.setBounds(250, 250, 440, 500);
            this.setLayout(null);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }    public static void main(String[] args) {
            new CodeCount().indexMain();
        }    public void indexMain() {
            jl_version = new JLabel();
            jl_version.setText("统计代码的一个小程序----by Reeves ");
            jl_version.setBounds(220, 430, 300, 30);
            this.add(jl_version);        // 选择文件按钮
            jbtn_openfile = new JButton("文件");
            jbtn_openfile.setBounds(50, 20, 80, 25);
            jbtn_openfile.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    openFiles();
                }
            });
            this.add(jbtn_openfile);
            // 开始按钮
            jbtn_begin = new JButton("计算");
            jbtn_begin.setBounds(jbtn_openfile.getX(), jbtn_openfile.getY() + 30,
                    80, 25);
            this.add(jbtn_begin);
            jbtn_begin.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    for (File child : files) {
                     normalLines=0;            //********
                     commentLines=0;           //*******
                     whiteLines =0;            //********
                        if (child.getName().matches(".*\\.java$")) {
                            String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
                            parse(child);
                            fname = child.getName();// 文件名
                            flength = String.valueOf(child.length());// 文件大小
                            nocode = String.valueOf(normalLines);
                            cocode = String.valueOf(commentLines);
                            whcode = String.valueOf(whiteLines);
                            codetotal = String.valueOf(normalLines + commentLines
                                    + whiteLines);
                            listFiles(fname, flength, nocode, cocode, whcode,
                                    codetotal);
                        } else if (child.getName().matches(".*\\.txt$")) {
                            String fname = null, flength = null, nocode = null, cocode = null, whcode = null, codetotal = null;
                            parse(child);
                            fname = child.getName();// 文件名
                            flength = String.valueOf(child.length());// 文件大小
                            nocode = String.valueOf(normalLines);
                            cocode = String.valueOf(commentLines);
                            whcode = String.valueOf(whiteLines);
                            codetotal = String.valueOf(normalLines + commentLines
                                    + whiteLines);
                            listFiles(fname, flength, nocode, cocode, whcode,
                                    codetotal);
                        }
                    }
                    tatalNormal+=normalLines;               //********
                    tatalComment+= commentLines;            //*******
                    tatalWhite+= whiteLines;                //*******
                    result(tatalNormal, tatalComment, tatalWhite);  //*********
                }
            });        // 值标签
            jl_normalLines = new JLabel("有效代码:");
            jl_commentLines = new JLabel("注         释:");
            jl_whiteLines = new JLabel("空         行:");
            jl_normalLines.setBounds(jbtn_openfile.getX() + 120, jbtn_openfile
                    .getY() - 5, 120, 30);
            jl_commentLines.setBounds(jbtn_openfile.getX() + 120, jbtn_openfile
                    .getY() - 5 + 50, 120, 30);
            jl_whiteLines.setBounds(jbtn_openfile.getX() + 120, jbtn_openfile
                    .getY() - 5 + 90, 120, 30);        jl_ntext = new JLabel();
            jl_ctext = new JLabel();
            jl_wtext = new JLabel();
            jl_ntext.setBounds(jbtn_openfile.getX() + 205,
                    jbtn_openfile.getY() - 5, 80, 30);
            jl_ctext.setBounds(jbtn_openfile.getX() + 205,
                    jbtn_openfile.getY() - 5 + 50, 80, 30);
            jl_wtext.setBounds(jbtn_openfile.getX() + 205,
                    jbtn_openfile.getY() - 5 + 90, 80, 30);
            this.add(jl_ntext);
            this.add(jl_ctext);
            this.add(jl_wtext);
            // 清空按钮
            jbtn_clear = new JButton("清空");
            jbtn_clear.setBounds(jbtn_openfile.getX(), jbtn_openfile.getY() + 60,
                    80, 25);
            jbtn_clear.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    jl_ntext.setText(null);
                    jl_ctext.setText(null);
                    jl_wtext.setText(null);
                    tatalNormal= 0;           //********
                    tatalComment = 0;        //******
                    tatalWhite= 0;            //*********
                }
            });
            this.add(jbtn_clear);
            this.add(jl_normalLines);
            this.add(jl_commentLines);
            this.add(jl_whiteLines);        // 详细按钮
            jbtn_list = new JButton("详细");
            jbtn_list.setBounds(jbtn_openfile.getX(), jbtn_openfile.getY() + 90,
                    80, 25);
            this.add(jbtn_list);
            // 文件清单        dtm = new DefaultTableModel(fileinfo, tablehead);
            view = new JTable(dtm);
            view.setAutoCreateRowSorter(true);
            jsc_list = new JScrollPane(view);
            jsc_list.setBounds(jbtn_openfile.getX() - 20,
                    jbtn_openfile.getY() + 130, 380, 280);
            this.add(jsc_list);
            this.setVisible(true);
        }    public void result(long a, long b, long c) {
            jl_ntext.setText(String.valueOf(a) + "行");
            jl_ctext.setText(String.valueOf(b) + "行");
            jl_wtext.setText(String.valueOf(c) + "行");
        }    // 文件详细清单
        public void listFiles(String n1, String n2, String n3, String n4,
                String n5, String n6) {
            Vector<String> v = new Vector<String>();
            v.add(n1);
            v.add(n2);
            v.add(n3);
            v.add(n4);
            v.add(n5);
            v.add(n6);
            dtm.addRow(v);
        }    // 选择文件对话框
        public void openFiles() {
            chooser = new JFileChooser(
                    JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY);
            chooser.setDialogTitle("选择文件");        // 过滤文件
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "TXT&JAVA", "txt", "java");
            chooser.setFileFilter(filter);
            chooser.setMultiSelectionEnabled(true);
            chooser.showOpenDialog(this);
            files = chooser.getSelectedFiles();
        }    // 业务逻辑
        private static void parse(File f) {
            BufferedReader br = null;
            boolean comment = false;
            try {
                br = new BufferedReader(new FileReader(f));
                String line = "";
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (line.matches("^[\\s&&[^\\n]]*$")) {
                        whiteLines++;
                    } 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 {
                        normalLines++;
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                        br = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }