try {
long startTime = System.currentTimeMillis();
// 要讀取的文件
FileInputStream fin = new FileInputStream(\”/Applications/demo/aaaa.txt\”);
FileChannel fcin = fin.getChannel();
// 文件過大拆分成 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 \”秒\”);
// 分割成了 每一個文件 大小是 128M, 然后在用傳統的方法去讀取即可
} catch (Exception e) {
}
}
關于JAVA中怎么讀取大TXT文件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn