JavaNIO性能测试的方法是什么

这篇文章主要介绍“Java NIO性能测试的方法是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java NIO性能测试的方法是什么”文章能帮助大家解决问题。

定陶网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联公司于2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

时间(ms)

文件大小(byte)

Buffer(byte)

434

603900

10000

0

0

1000

0

46

100

0

188

50

0

281

5

0

2406

1

47

12000

java 代码:

package com;           import java.io.File;      import java.io.FileInputStream;      import java.io.FileOutputStream;      import java.io.IOException;      import java.nio.ByteBuffer;      import java.nio.channels.FileChannel;           import junit.framework.TestCase;           /**      * NIO read write test      *       * @author wutao      *       */     public class NioDemo extends TestCase {               public void testRead() throws IOException {                   int[] sizes = { 10000, 1000, 100, 50, 5, 1 };                   // Arrays.sort(sizes);                   System.out.println(new File("text.txt").length());                   for (int i = 0; i < sizes.length; i++) {                       int size = sizes[i];                       FileInputStream fins = new FileInputStream("text.txt");                       FileChannel fc = fins.getChannel();                       if (!new File("text2.txt").exists()) {                      new File("text2.txt").createNewFile();                  }                  ByteBuffer buffer = ByteBuffer.allocate(size);                       FileOutputStream fouts = new FileOutputStream("text2.txt");                  FileChannel fc2 = fouts.getChannel();                       long start = System.currentTimeMillis();                       while (true) {                      buffer.clear();                      int r = fc.read(buffer);                      if (r == -1) {                          break;                      }                      buffer.flip();                      fc2.write(buffer);                  }                       long end = System.currentTimeMillis();                       System.out.println("---------" + size + "---------");                  System.out.println(end - start);                  fc.close();                  fc2.close();                  fins.close();                  fouts.close();              }          }      }
Java™ I/O, 2nd Edition  By Elliotte Rusty Harold   ...............................................   Publisher: O'Reilly   Pub Date: May 2006   Print ISBN-10: 0-596-52750-0   Print ISBN-13: 978-0-59-652750-1   Pages: 726
import java.io.*;     import java.nio.*;     import java.nio.channels.*;     public class NIOCopier {       public static void main(String[] args) throws IOException {         FileInputStream inFile = new FileInputStream(args[0]);         FileOutputStream outFile = new FileOutputStream(args[1]);         FileChannel inChannel = inFile.getChannel( );         FileChannel outChannel = outFile.getChannel( );         for (ByteBuffer buffer = ByteBuffer.allocate(1024*1024);         inChannel.read(buffer) != -1;         buffer.clear( )) {           buffer.flip( );           while (buffer.hasRemaining( )) outChannel.write(buffer);         }         inChannel.close( );         outChannel.close( );       }     }

关于“Java NIO性能测试的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。


本文题目:JavaNIO性能测试的方法是什么
文章出自:http://myzitong.com/article/pdjppi.html