try {
long startTime = System.currentTimeMillis();
// 要讀取的文件
FileInputStream fin = new FileInputStream(\”/Applications/demo/aaaa.txt\”);
FileChannel fcin = fin.getChannel();
// 文件過(guò)大拆分成 128M 大小的txt文件
ByteBuffer buffer = ByteBuffer.allocate(128 * 1024 * 1024);
while (true) {
buffer.clear();
int flag = fcin.read(buffer);
if (flag == -1) {
break;
}
buffer.flip();
// 輸出到指定的位置
FileOutputStream fileInputStream = new FileOutputStream(\”/Applications/demo/\” UUID.randomUUID().toString() \”.txt\”);
FileChannel channel = fileInputStream.getChannel();
channel.write(buffer);
}
long endTime = System.currentTimeMillis();
System.out.println(\” 共消耗:\” (endTime-startTime) / 1000 \”秒\”);
// 分割成了 每一個(gè)文件 大小是 128M, 然后在用傳統(tǒng)的方法去讀取即可
} catch (Exception e) {
}
}
關(guān)于JAVA中怎么讀取大TXT文件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)西部數(shù)碼官網(wǎng):m.ps-sw.cn