Files.readLines(new File(path), Charsets.UTF_8)FileUtils.readLines(new File(path))
这种方法带来的问题是文件的所有行都被存放在内存中,当文件足够大时很快就会导致程序抛出OutOfMemoryError 异常。
import java.io.Fileimport java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
public class ReadWriteCompare
{
public static void main(String[] args) throws IOException
{
FileInputStream fileInputStream = new FileInputStream("f:"+ File.separator +"IBM e-Mentor Program Kickoff Night 1105.pdf")
FileOutputStream fileOutputStream = new FileOutputStream("f:" + File.separator + "test.pdf")
FileChannel inChannel = fileInputStream.getChannel()
FileChannel outChannel= fileOutputStream.getChannel()
ByteBuffer byteBuffer = ByteBuffer.allocate(1024)
//Direct Buffer的效率会更高。
// ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024)
long start = System.currentTimeMillis()
while(true)
{
int eof = inChannel.read(byteBuffer)
if(eof == -1 ) break
byteBuffer.flip()
outChannel.write(byteBuffer)
byteBuffer.clear()
}
System.out.println("spending : " + (System.currentTimeMillis()-start))
inChannel.close()
outChannel.close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)