在Java中讀寫大文件時(shí),可以使用如下方法:
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"));
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bis.close();
bos.close();
FileChannel inChannel = new FileInputStream("input.txt").getChannel();
FileChannel outChannel = new FileOutputStream("output.txt").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(8192);
while (inChannel.read(buffer) != -1) {
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
inChannel.close();
outChannel.close();
File sourceFile = new File("input.txt");
File destFile = new File("output.txt");
FileUtils.copyFile(sourceFile, destFile);
通過以上方法,可以在Java中高效地讀寫大文件。需要根據(jù)具體情況選擇最適合的方法。