Java方法讀取文件內容


一.針對文件內容的讀取,在平時的工作中想必是避免不了的操作,現在我將自己如何用java方法讀取文件中內容總結如下:廢話不多說,直接上代碼:

 1 public static void main(String[] args) throws IOException {
 2         FileInputStream fileInputStream = null;
 3         try {
 4             // 1.獲取文件指定的文件信息
 5             fileInputStream = new FileInputStream("D:\\softwore\\workspace\\springbootdemo\\node10-boot-mybatis\\src\\main\\resources\\test.txt");
 6             // 2.將數據讀到字節數組里
 7             byte[] buff = new byte[1024];
 8             int length = fileInputStream.read(buff);
 9             // 3.將字節數據轉換為字符串
10             // 參數一:帶轉換的字節數組,參數二:起始位置  參數三:轉換的長度
11             String info = new String(buff, 0, length);
12             System.out.println(info);
13         } catch (IOException e) {
14             e.printStackTrace();
15         } finally {
16             // 4,關閉流操作
17             if (fileInputStream != null)
18             fileInputStream.close();
19         }

二.執行結果如下

1.文件存放位置

2.結果如下:

 


免責聲明!

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



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