import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test {
public static void main(String[] args) throws Exception{
Long startTime = System.currentTimeMillis();
testByteBuffer();
Long endTime = System.currentTimeMillis();
// 计算并打印耗时
Long tempTime = (endTime - startTime);
System.out.println("花费时间:"+
(((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
((tempTime%1000)+"ms"));
}
static File file = new File("E:\\Linux【更多IT资源加微信wxdhhfy】\\Linux\\hadoop000-Snapshot1.vmem");
static int bufferSize = 1024;
public static void testByteBuffer() throws IOException {
FileChannel channel = new FileInputStream(file).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
int bytes;
while ((bytes = channel.read(buffer)) != -1) {
byte[] data = new byte[bytes];
buffer.flip();
buffer.get(data);
buffer.clear();
}
channel.close();
}
}
控制变量,通过设置VM options参数-Xmx
-Xmx 为JVM运行时可申请的最大Heap值,默认值为物理内存的1/4但小于1G,默认当空余堆内存小于40%时,JVM会增大Heap到-Xmx指定的大小,可通过-XX:MinHeapFreeRation来指定这个比列。
同事的解决方法是使用BufferedReader,因为其中同样有缓冲区,还有readLine方法
解析大文件,并存到mq中,在同一个服务中使用多线程进行消费,存到es中