简单小程序----代码行数计数器

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 
 7 
 8 
 9 public class CodeCounter {
10 
11     static int normalLines = 0;//普通行
12     static int commentLines = 0;//注释行
13     static int blankLines = 0;//空行
14     
15     
16     public static void main(String[] args) {
17         
18         File f = new File("D:/code");
19         File[] codeFile = f.listFiles();
20         for(File child :codeFile){
21             
22         if(child.getName().matches(".*\\.java$"));{
23             parse(child);
24         }
25         }
26         
27         
28         
29         System.out.println("normalLines = " + normalLines);
30         System.out.println("commentLines = " + commentLines);
31         System.out.println("blankLines = " + blankLines);
32     }
33 
34 
35     private static void parse(File f) {
36         BufferedReader br = null;
37         boolean comment = false;
38         try {
39             br = new BufferedReader(new FileReader(f));
40             String line = "";
41             while((line = br.readLine())!=null){
42                 line = line.trim();
43                 if(line.matches("^[\\s&&[^\\n]]*")){
44                     blankLines++;
45                 }else if(line.matches("/*")&&!line.endsWith("*/")){
46                     commentLines++;
47                     comment = true;
48             
49                 }else if( true == comment){
50                     commentLines++;
51                     if(line.endsWith("*/")){
52                         comment = false;
53                     }
54                 }else{
55                     normalLines++;
56                 }
57                 
58             }
59             
60         } catch (FileNotFoundException e) {
61             // TODO Auto-generated catch block
62             e.printStackTrace();
63         } catch (IOException e) {
64             // TODO Auto-generated catch block
65             e.printStackTrace();
66         }finally{
67             if(br!=null){
68                 try {
69                     br.close();
70                     br= null;
71                 } catch (IOException e) {
72                     // TODO Auto-generated catch block
73                     e.printStackTrace();
74                 }
75             }
76             
77         }
78         
79     }
80 
81 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中,可以使用 `java.io.File` 类来获取文件的行数和大小。 获取文件的行数: 1. 使用 `java.io.BufferedReader` 和 `java.io.FileReader` 类打开文件,然后逐行读取文件,累加行数代码示例如下: ``` import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { File file = new File("example.txt"); int lineCount = 0; try (BufferedReader reader = new BufferedReader(new FileReader(file))) { while (reader.readLine() != null) { lineCount++; } } System.out.println("Number of lines: " + lineCount); } } ``` 获取文件的大小: 1. 使用 `java.io.File` 类的 `length()` 方法获取文件的大小,代码示例如下: ``` import java.io.File; public class Main { public static void main(String[] args) { File file = new File("example.txt"); long size = file.length(); System.out.println("Size of file: " + size + " bytes"); } } ``` ### 回答2: 要使用java的File类获取文件的行数和大小,可以按照以下步骤进行操作: 1. 首先,需要创建一个File对象,指向要获取行数和大小的文件。可以使用File类的构造方法,传入文件的路径作为参数。 2. 为了获取文件的行数,可以使用BufferedReader类。首先创建一个BufferedReader对象,并传入一个FileReader对象作为参数。FileReader对象用于读取文件内容。 3. 创建一个计数器变量,用于统计文件的行数。初始化为0。 4. 使用BufferedReader对象的readLine()方法逐行读取文件内容,每读取一行,将计数器变量加1。 5. 当读取到文件的末尾时,readLine()方法会返回null,此时可以停止读取,得到文件的行数。 6. 获取文件的大小是通过File类的length()方法来实现的。调用File对象的length()方法即可得到文件的字节大小。 7. 最后,将得到的文件行数和大小进行输出或者保存。 下面是示例代码: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileSizeAndLineCounter { public static void main(String[] args) { File file = new File("文件路径"); // 替换成实际文件路径 // 获取文件的行数 int lineCount = 0; try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { lineCount++; } } catch (IOException e) { e.printStackTrace(); } // 获取文件的大小 long fileSize = file.length(); System.out.println("文件的行数:" + lineCount); System.out.println("文件的大小:" + fileSize + "字节"); } } ``` 使用上述代码,可以通过File类获取文件的行数和大小。需要注意的是,要替换代码中的"文件路径"部分为你实际的文件路径。 ### 回答3: 要使用Java中的File类来获取文件的行数和大小,可以按照以下步骤进行操作: 1. 首先,创建一个File对象,指定要获取信息的文件路径。 2. 使用FileReader类和BufferedReader类,以行为单位读取文件内容。定义一个计数器变量lineCount来记录行数。 3. 在循环中,使用BufferedReader的readLine()方法读取每一行内容,如果读取到的内容不为空,则行数加1。 4. 完成循环后,lineCount变量即为文件的行数。 5. 使用File对象的length()方法,可以获取文件的大小,以字节为单位。 6. 最后,将得到的行数和文件大小进行输出或存储等后续处理。 以下是示例代码: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileUtil { public static void main(String[] args) { String filePath = "文件路径"; // 输入要获取信息的文件路径 File file = new File(filePath); int lineCount = getLineCount(file); long fileSize = file.length(); System.out.println("文件行数:" + lineCount); System.out.println("文件大小:" + fileSize + "字节"); } public static int getLineCount(File file) { int lineCount = 0; try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { lineCount++; } } catch (IOException e) { e.printStackTrace(); } return lineCount; } } ``` 上述代码中,需要将"文件路径"替换为实际的文件路径,然后运行程序即可获取文件的行数和大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值