Java NIO
프레임 워크를 사용하여 대용량 파일에서 효과적으로 읽고 대량 데이터를 파일에 쓸 수 있습니다.Java NIO를 사용하여 큰 파일 읽기 및 쓰기
나는 ByteBuffer
과 FileChannel
함께 일하고 있어요 아래 같은 것을 시도했다 :
public static void main(String[] args)
{
String inFileStr = "screen.png";
String outFileStr = "screen-out.png";
long startTime, elapsedTime;
int bufferSizeKB = 4;
int bufferSize = bufferSizeKB * 1024;
// Check file length
File fileIn = new File(inFileStr);
System.out.println("File size is " + fileIn.length() + " bytes");
System.out.println("Buffer size is " + bufferSizeKB + " KB");
System.out.println("Using FileChannel with an indirect ByteBuffer of " + bufferSizeKB + " KB");
try ( FileChannel in = new FileInputStream(inFileStr).getChannel();
FileChannel out = new FileOutputStream(outFileStr).getChannel())
{
// Allocate an indirect ByteBuffer
ByteBuffer bytebuf = ByteBuffer.allocate(bufferSize);
startTime = System.nanoTime();
int bytesCount = 0;
// Read data from file into ByteBuffer
while ((bytesCount = in.read(bytebuf)) > 0) {
// flip the buffer which set the limit to current position, and position to 0.
bytebuf.flip();
out.write(bytebuf); // Write data from ByteBuffer to file
bytebuf.clear(); // For the next read
}
elapsedTime = System.nanoTime() - startTime;
System.out.println("Elapsed Time is " + (elapsedTime/1000000.0) + " msec");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
사람이 있다면이 같은 절차를 따라야합니다, 말할 수 2 GB 이상의 내 파일 크기?
서면 작업이 대량 인 경우 작문하는 동안 비슷한 일을하고 싶다면 어떻게해야합니까?
자바 7. 이후
FileInputStream
/FileOutputStream
우회하지 않고 open aFileChannel
directly 당신은 사이에서 데이터를 처리하려고하거나 그냥 복사 할 수 있습니까? – Kayaman'비슷한 일을하고 싶다면'은 무엇을 의미합니까? – EJP