Java里的IO流里的FileInputStream 的讀取並在前打印行數!


大家好!!新人求罩!

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 
 7 public class work6 {
 8 
 9     /**
10      * @param args
11      * @throws IOException 
12      */
13     public static void main(String[] args) throws IOException {
14         // TODO Auto-generated method stub
15         File file = new File("C:\\Users\\wang\\Desktop\\io作業.txt");//文件的地址!!!
16         //System.out.println(file.exists());
17         FileInputStream in = new FileInputStream(file);
18         
19         byte[] b = new byte[1024];
20         
21         int c = 0;
22         
23         String str = "";
24         
25         while((c = in.read(b)) != -1){
26              str += new String(b,0,c);//在此拼接
27         }
28         String[] s = str.split("\n");//以\n來截取!轉化為數組!
29         for(int i = 0; i < s.length; i++){//遍歷當前數組!
30             System.out.print((i+1)+"\t"+s[i]);
31         }
32     }
33 
34 }

一個簡單的讀取就完成了!!就是這么簡單!


免責聲明!

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



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