讀取檔案有多種方式,基於傳統的輸入流方式或基於nio的Buffer緩衝物件和管道讀取方式甚至非常快速的記憶體映射讀取檔案。
java中四種讀取檔案方式:(建議:java影片教學)
#1、RandomAccessFile:隨機讀取,比較慢優點就是該類別可讀可寫入可操作檔案指標
2、FileInputStream:io普通輸入流方式,速度效率一般
3、Buffer緩衝讀取:基於nio Buffer和FileChannel讀取,速度較快
4、記憶體映射讀取:基於MappedByteBuffer,速度最快
#RandomAccessFile讀取
//RandomAccessFile类的核心在于其既能读又能写 public void useRandomAccessFileTest() throws Exception { RandomAccessFile randomAccessFile = new RandomAccessFile(new File("e:/nio/test.txt"), "r"); byte[] bytes = new byte[1024]; int len = 0; while ((len = randomAccessFile.read(bytes)) != -1) { System.out.println(new String(bytes, 0, len, "gbk")); } randomAccessFile.close(); }
##FielInputStream讀取
//使用FileInputStream文件输入流,比较中规中矩的一种方式,传统阻塞IO操作。 public void testFielInputStreamTest() throws Exception { FileInputStream inputStream = new FileInputStream(new File("e:/nio/test.txt")); // 使用输入流读取文件,以下代码块几乎就是模板代码 byte[] bytes = new byte[1024]; int len = 0; while ((len = inputStream.read(bytes)) != -1) {// 如果有数据就一直读写,否则就退出循环体,关闭流资源。 System.out.println(new String(bytes, 0, len, "gbk")); } inputStream.close(); }
Buffer緩衝物件讀取
// nio 读取 public void testBufferChannel() throws Exception { FileInputStream inputStream = new FileInputStream(new File("e:/nio/test.txt")); FileChannel fileChannel = inputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); // 以下代码也几乎是Buffer和Channle的标准读写操作。 while (true) { buffer.clear(); int result = fileChannel.read(buffer); buffer.flip(); if (result == -1) { break; } System.out.println(new String(buffer.array(), 0, result, "gbk")); } inputStream.close(); }
記憶體映射讀取
public void testmappedByteBuffer() throws Exception { FileInputStream inputStream = new FileInputStream(new File("e:/nio/test.txt")); FileOutputStream outputStream = new FileOutputStream(new File("e:/nio/testcopy.txt"),true); FileChannel inChannel = inputStream.getChannel(); FileChannel outChannel = outputStream.getChannel(); System.out.println(inChannel.size()); MappedByteBuffer mappedByteBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size()); System.out.println(mappedByteBuffer.limit()); System.out.println(mappedByteBuffer.position()); mappedByteBuffer.flip(); outChannel.write(mappedByteBuffer); outChannel.close(); inChannel.close(); outputStream.close(); inputStream.close(); } //基于内存映射这种方式,这么写好像有问题。 MappedByteBuffer和RandomAcessFile这两个类要单独重点研究一下。 //TODO 大文件读取更多java知識請關注
java基礎教程專欄。
以上是java中如何讀取檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3漢化版
中文版,非常好用

Atom編輯器mac版下載
最受歡迎的的開源編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中