无聊,把F盘写满。。。

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.util.UUID;
 7 
 8 
 9 public class Filled{
10 
11     static final int BUFFER_SIZE = 10 * 1024;
12     static final long FREE_SPACE_SIZE = 200 * 1024;
13     public static void main(String[] args) {
14         File srcFile = new File("F:\\office.iso");
15         File tarFile = null;
16         FileInputStream fis = null;
17         FileOutputStream fos = null;
18         File discFile = new File("F:\\");
19         System.out.println("F盘剩余容量:" + discFile.getFreeSpace()/(1024*1024) + "M");
20         while(discFile.getFreeSpace() > FREE_SPACE_SIZE){
21         
22             tarFile = new File("F:\\"+UUID.randomUUID()+".iso");            
23             try {
24                 tarFile.createNewFile();
25                 fis = new FileInputStream(srcFile);
26                 fos = new FileOutputStream(tarFile);
27                 byte[] buff = new byte[BUFFER_SIZE];
28                 int n = 0;
29                 long currTime = System.currentTimeMillis();
30                 while(-1 !=(n = fis.read(buff,0,BUFFER_SIZE))){
31                     fos.write(buff, 0, n);
32                     fos.flush();
33                 }
34                 long wastTime = System.currentTimeMillis() - currTime;
35                 System.out.println(wastTime);
36                 fos.close();
37             } catch (FileNotFoundException e) {
38                 // TODO Auto-generated catch block
39                 e.printStackTrace();
40             } catch (IOException e) {
41                 // TODO Auto-generated catch block
42                 System.out.println(discFile.getFreeSpace());
43                 System.out.println("F盘剩余容量:" + discFile.getFreeSpace()/(1024*1024) + "M");
44                 
45                 
46             }finally{
47                 if(fis != null){
48                     try {
49                         fis.close();
50                     } catch (IOException e) {
51                         // TODO Auto-generated catch block
52                         e.printStackTrace();
53                     }
54                 }
55                 if(fos != null){
56                     try {
57                         fos.close();
58                     } catch (IOException e) {
59                         // TODO Auto-generated catch block
60                         e.printStackTrace();
61                     }
62                 }
63             }
64             System.out.println("F盘剩余容量:" + discFile.getFreeSpace()/(1024*1024) + "M");
65         }        
66     }
67 }

 

posted @ 2013-07-26 20:57  抽屜裡的秘密  阅读(215)  评论(0)    收藏  举报