public static void readBigFile(String filepath){
final int BUFFER_SIZE = 0x1200000; // 缓冲区为3M
Long len = 0l;
String temp = "";
Long start = System.currentTimeMillis();
try {
File f = new File(filepath);
for (int z = 8; z >0; z--) {
MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")
.getChannel().map(FileChannel.MapMode.READ_ONLY,
f.length() * (z-1) / 8, f.length() * 1 / 8);
byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容
for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {
if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {
for (int i = 0; i < BUFFER_SIZE; i++)
dst[i] = inputBuffer.get(offset + i);
} else {
for (int i = 0; i < inputBuffer.capacity() - offset; i++)
dst[i] = inputBuffer.get(offset + i);
}
int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
: inputBuffer.capacity() % BUFFER_SIZE;
temp = new String(dst, 0, length);
len += temp.length();
System.out.println(temp);
System.out.println(temp.length()+"-"+(z-1)+"-"+(8-z+1));
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(len);
long end = System.currentTimeMillis();
System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
}
public static void readBigFile2(String filepath) {
try {
Long start = System.currentTimeMillis();
FileInputStream fin = new FileInputStream(filepath);
FileChannel fcin = fin.getChannel();
Long len = 0l;
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 100);
// FileOutputStream fout = new FileOutputStream(Math.random() + ".log");
while(true)
{
buffer.clear();
int flag = fcin.read(buffer);
if(flag == -1)
{
break;
}
buffer.flip();
// FileChannel fcout = fout.getChannel();
///
len += buffer.getInt();
// fcout.write(buffer);
}
long end = System.currentTimeMillis();
System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
} catch (Exception e) {
e.printStackTrace();
}
}
final int BUFFER_SIZE = 0x1200000; // 缓冲区为3M
Long len = 0l;
String temp = "";
Long start = System.currentTimeMillis();
try {
File f = new File(filepath);
for (int z = 8; z >0; z--) {
MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")
.getChannel().map(FileChannel.MapMode.READ_ONLY,
f.length() * (z-1) / 8, f.length() * 1 / 8);
byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容
for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {
if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {
for (int i = 0; i < BUFFER_SIZE; i++)
dst[i] = inputBuffer.get(offset + i);
} else {
for (int i = 0; i < inputBuffer.capacity() - offset; i++)
dst[i] = inputBuffer.get(offset + i);
}
int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
: inputBuffer.capacity() % BUFFER_SIZE;
temp = new String(dst, 0, length);
len += temp.length();
System.out.println(temp);
System.out.println(temp.length()+"-"+(z-1)+"-"+(8-z+1));
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(len);
long end = System.currentTimeMillis();
System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
}
public static void readBigFile2(String filepath) {
try {
Long start = System.currentTimeMillis();
FileInputStream fin = new FileInputStream(filepath);
FileChannel fcin = fin.getChannel();
Long len = 0l;
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 100);
// FileOutputStream fout = new FileOutputStream(Math.random() + ".log");
while(true)
{
buffer.clear();
int flag = fcin.read(buffer);
if(flag == -1)
{
break;
}
buffer.flip();
// FileChannel fcout = fout.getChannel();
///
len += buffer.getInt();
// fcout.write(buffer);
}
long end = System.currentTimeMillis();
System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
} catch (Exception e) {
e.printStackTrace();
}
}