我用JAVA NIO 来复制文件,但没有发现和传统的流的方式来操作有明显的优势,为什么呢?
importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;impor...
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
public class TestPerformance {
static File srcFile = new File("K:/新宿事件BD中字1024x556高清版.rmvb");
static File destFile = new File("K:/test/test.rmvb");
/**
* @param args
*/
public static void main(String[] args) throws Exception {
testTraditional();
testNIO();
}
private static void testNIO()throws Exception{
long start = System.currentTimeMillis();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024 * 20);
initDestFile();
FileInputStream fin = new FileInputStream(srcFile);
FileOutputStream fout = new FileOutputStream(destFile);
FileChannel srcChannel = fin.getChannel();
FileChannel destChannel = fout.getChannel();
while (srcChannel.read(buffer) != -1) {
buffer.flip();
while(buffer.hasRemaining()) {
destChannel.write(buffer);
}
buffer.clear();
}
srcChannel.close();
destChannel.close();
fin.close();
fout.close();
long end = System.currentTimeMillis();
System.out.println("Cost in NIO: " + (end - start));
}
private static void testTraditional() throws Exception {
long start = System.currentTimeMillis();
byte [] buffer = new byte[1024 * 1024 * 20];
initDestFile();
FileInputStream fin = new FileInputStream(srcFile);
FileOutputStream fout = new FileOutputStream(destFile);
int readedSize = 0;
while ((readedSize = fin.read(buffer)) > 0) {
fout.write(buffer, 0, readedSize - 1);
}
fin.close();
fout.close();
long end = System.currentTimeMillis();
System.out.println("Cost in tranditional: " + (end - start));
}
private static void initDestFile() throws Exception {
if (destFile.exists()) {
destFile.delete();
}
destFile.createNewFile();
}
}
进过测试发现这两种操作方式花费的时间差不多,是不是我的用法不对呢?
阁下说的答案不对,我问的是为什么NIO我用了之后没有发现比传统的IO要快很多 为什么? 请能者回答。谢谢 展开
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
public class TestPerformance {
static File srcFile = new File("K:/新宿事件BD中字1024x556高清版.rmvb");
static File destFile = new File("K:/test/test.rmvb");
/**
* @param args
*/
public static void main(String[] args) throws Exception {
testTraditional();
testNIO();
}
private static void testNIO()throws Exception{
long start = System.currentTimeMillis();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024 * 20);
initDestFile();
FileInputStream fin = new FileInputStream(srcFile);
FileOutputStream fout = new FileOutputStream(destFile);
FileChannel srcChannel = fin.getChannel();
FileChannel destChannel = fout.getChannel();
while (srcChannel.read(buffer) != -1) {
buffer.flip();
while(buffer.hasRemaining()) {
destChannel.write(buffer);
}
buffer.clear();
}
srcChannel.close();
destChannel.close();
fin.close();
fout.close();
long end = System.currentTimeMillis();
System.out.println("Cost in NIO: " + (end - start));
}
private static void testTraditional() throws Exception {
long start = System.currentTimeMillis();
byte [] buffer = new byte[1024 * 1024 * 20];
initDestFile();
FileInputStream fin = new FileInputStream(srcFile);
FileOutputStream fout = new FileOutputStream(destFile);
int readedSize = 0;
while ((readedSize = fin.read(buffer)) > 0) {
fout.write(buffer, 0, readedSize - 1);
}
fin.close();
fout.close();
long end = System.currentTimeMillis();
System.out.println("Cost in tranditional: " + (end - start));
}
private static void initDestFile() throws Exception {
if (destFile.exists()) {
destFile.delete();
}
destFile.createNewFile();
}
}
进过测试发现这两种操作方式花费的时间差不多,是不是我的用法不对呢?
阁下说的答案不对,我问的是为什么NIO我用了之后没有发现比传统的IO要快很多 为什么? 请能者回答。谢谢 展开
展开全部
好好读读Thinking in java文档,从1.5开始,Java对InputStream/OutputStream 进行了重新改写,用的就是NIO,因此,就算你不显示声明要用NIO,只要你的类继承了InputStream/OutputStream就已经在用NIO了,不信的话这样做
FileChannel channel=new FileInputStream.getChannel();
如果XXStream不用NIO构造,如何返回一个Channel的对象?
FileChannel channel=new FileInputStream.getChannel();
如果XXStream不用NIO构造,如何返回一个Channel的对象?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询