(I/O流)在100ms内桌面上生成一个200M大小的文件

最终速度取决于硬盘的读写速度

 1 package com.laurdawn;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class test {
 9 
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         File f1 = new File("C:/Users/Administrator/Desktop/test.txt");
13         try {
14             FileOutputStream fos = new FileOutputStream(f1);
15             if (!f1.exists()) {
16                 try {
17                     f1.createNewFile();
18                 } catch (IOException e) {
19                     // TODO Auto-generated catch block
20                     e.printStackTrace();
21                 }
22             }
23             byte[] buffer = new byte[1024 * 200];
24             long start = System.currentTimeMillis();
25             for (int i = 0; i < 1024; i++) {
26                 fos.write(buffer);
27                 fos.flush();
28 
29             }
30             long end = System.currentTimeMillis();
31             long time = end - start;
32             System.out.println("写入时间:" + time + "ms");
33             fos.close();
34         } catch (IOException e1) {
35             // TODO Auto-generated catch block
36             e1.printStackTrace();
37         }
38     }
39 
40 }

 

posted @ 2016-07-01 09:26  laurdawn  阅读(579)  评论(0编辑  收藏  举报