統計一個文本文件中字符的個數//統計文件中文本的行數


 1 //統計一個文本文件中字符的個數
 2 package classwork10;
 3 
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 
 7 public class Tj {
 8 
 9     public static void main(String[] args) throws IOException {
10         FileInputStream f = new FileInputStream("D:/dssjava/abc.txt");
11         //byte[] buf = new byte[f.available()];
12         //System.out.println("文件的字節個數:" + f.read(buf));
13         int len,count=0;
14         while ((len =f.read())!= -1) {
15               if(len!=(int)'\n'&&len!=(int)'\r') {
16                   count++;
17               }
18         }
19         System.out.println("文件的字符個數:" + count);
20     }
21 
22 }
 1 //統計文件中文本的行數
 2 package classwork10;
 3 
 4 import java.io.BufferedReader;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 
 8 public class Tj1 {
 9 
10     public static void main(String[] args) throws IOException {
11         FileReader fr=new FileReader("D:/dssjava/abc.txt");
12         BufferedReader bw=new BufferedReader(fr);
13         int count=0;
14         String len="";
15         while((len=bw.readLine())!=null) {
16             count++;
17         }
18         System.out.println("文件的行數為:"+count);
19     }
20 
21 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM