BufferedReader in=new BufferedReader(new FileReader("StatNote.java"));
使用in.readline()读取每一行时 怎么判断读完整个文件的所有行啊(文件中本身含有空行)? 或各位大侠有没有什么好的方法:统计一个java源文件中注释的行数.欢迎给出源代码,谢谢!

解决方案 »

  1.   

    举个例子
    try{
         File f=new File("d:\\1.txt");
         fis=new BufferedReader(new FileReader(f));
         fi=new FileInputStream(f);
         //myDataStream=new DataInputStream(fis);
         in=new FileReader(f);
        
         }
         catch(FileNotFoundException e){
         System.out.println("hello");
         }
         try{
         while((s=fis.readLine())!=null){
                         System.out.println(s);
         }
      

  2.   

    文件中本身含有空行空行也是有内容的,不是null所以不必担心这个问题
      

  3.   

    import java.io.*;
    public class StatNote {
    private static int count=0;
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
         BufferedReader in=new BufferedReader(new FileReader("E:\\Note.java"));
         String s=new String(); 
            
         while((s=in.readLine())!=null){
          //统计用//方式注释的行数(排除在另外两种注释中使用了//符号的情况)
          if((!s.startsWith("/*")||!s.startsWith("*"))&&s.contains("//"))count++;
          
          //统计用/**....*/或/*...*/注释的行数
          if(s.startsWith("/*")||s.startsWith("*")){
            //如果注释没结束,继续统计  
            if(s.endsWith("*/"))count++; 
            else count++;     
         }
          in.close();
          System.out.println("程序中注释的行数为:"+count);
         }
        }
     
    }报错如下:程序中注释的行数为:1
    Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at StatNote.main(StatNote.java:13)
      

  4.   

    at StatNote.main(StatNote.java:13)
     也就是  while((s=in.readLine())!=null){ 
    是不是我的循环读取每行用得不对啊?
      

  5.   

    读取的时候你应该这么写:
    String s;
    while((s = in.readLine()) != null)no this : while(in.readLine() != null)这样的话,只能读奇数行
      

  6.   

    while((s=in.readLine())!=null){
    这么写好像是没有错的啊!还是不知道问题出现在哪了.
    哪位大侠能说明确点吗?
      

  7.   

    import java.io.*;
    public class StatNote {
    private static int count=0;
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) 
    {
    // TODO Auto-generated method stub
        try{ 
    BufferedReader in=new BufferedReader(new FileReader("E:\\Note.java"));
         String s=new String(); 
         boolean sw = false;
         System.out.println("hello");
         while((s=in.readLine())!=null)
         {
          //统计用//方式注释的行数(排除在另外两种注释中使用了//符号的情况)
          if(s.startsWith("/*"))
          {
          sw = true;
          }
        
          if(sw == true)
          {
          count++;
          if(s.endsWith("*/"))
              {
             sw = false;
              }
          }
          else if(s.contains("//"))
          {
          count++;
          }
          else ;
         }
         in.close();
        }
        catch(Exception e)
        {
         e.printStackTrace();
        }
        
       
        System.out.println("程序中注释的行数为:"+ count);
    }
    }
      

  8.   

    改一下楼上的,加入trim()就可以了,搂主,||和&&的优先级是不同的,比较保险的做法是先在把&&放前面import java.io.*;
    public class StatNote {
    private static int count=0;
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) 
    {
    // TODO Auto-generated method stub
        try{ 
    BufferedReader in=new BufferedReader(new FileReader("E:\\Note.java"));
         String s=new String(); 
         boolean sw = false;
         System.out.println("hello");
         while((s=in.readLine())!=null)
         {
          //统计用//方式注释的行数(排除在另外两种注释中使用了//符号的情况)
          if(s.trim().startsWith("/*"))
          {
          sw = true;
          }
        
          if(sw == true)
          {
          count++;
          if(s.trim().endsWith("*/"))
              {
             sw = false;
              }
          }
          else if(s.contains("//"))
          {
          count++;
          }
          else ;
         }
         in.close();
        }
        catch(Exception e)
        {
         e.printStackTrace();
        }
        
       
        System.out.println("程序中注释的行数为:"+ count);
    }
    }
      

  9.   

    自己的程序修改如下:
    import java.io.*;
    public class StatNote {
    private static int count=0;
    /**  
     * 统计程序注释的行数 
     * @author ruowang
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
         BufferedReader in=new BufferedReader(new FileReader("E:\\Note.java"));
         String s=new String();         
         while((s=in.readLine())!=null){     
         //统计用/**....*/或/*...*/注释的行数
         if(s.trim().startsWith("/*")||s.trim().startsWith("*")){
           //如果注释没结束,继续统计 
           if(s.trim().endsWith("*/"))count++; 
           else count++; 
          }
          //   统计用//方式注释的行数(排除了在另外两种注释中使用了//符号的情况)
           else if(s.trim().contains("//"))count++;
        }
         in.close();
         System.out.println("程序中注释的行数为:"+count);
         }
        }