文件拷贝

  Path path_from= Paths.get("d:\\深入剖析.pdf");

  Path path_to=Paths.get("d:\\深入剖析123.pdf");

  long startTime,endtime;

  try {

   FileChannel fileChannel_from=FileChannel.open(path_from, EnumSet.of(StandardOpenOption.READ));

   FileChannel fileChannel_to=FileChannel.open(path_to, EnumSet.of(StandardOpenOption.CREATE_NEW,StandardOpenOption.WRITE));

   startTime=System.currentTimeMillis();

   ByteBuffer byteBuffer=ByteBuffer.allocate(2048);

   int b;

   while ((b=fileChannel_from.read(byteBuffer))>0) {

    byteBuffer.flip();

    fileChannel_to.write(byteBuffer);

    byteBuffer.clear();

    

   }

   endtime=System.currentTimeMillis()-startTime;

   System.out.println(endtime);

  } catch (Exception e) {

   e.printStackTrace();

  }

你可能感兴趣的:(写入文件,读取文件,nio,拷贝文件)