KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > FileFlooder


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Jeremy Thomerson
5  *
6  * Cobertura is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License,
9  * or (at your option) any later version.
10  *
11  * Cobertura is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Cobertura; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */

21
22 package net.sourceforge.cobertura.util;
23
24 import java.io.File JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import org.apache.log4j.Logger;
29
30 /**
31  * I used this class to flood aa directory with files to test a fix for a guy
32  * that had a ton of files mixed with his code. You could use it to do similar
33  * for fixes / testing / misc.
34  *
35  * @author Jeremy Thomerson
36  */

37 public class FileFlooder
38 {
39
40     private static final Logger LOGGER = Logger.getLogger(FileFlooder.class);
41
42     public static void flood(String JavaDoc directory, String JavaDoc fileName, String JavaDoc fileExt, int numOfFiles,
43             int linesPerFile)
44     {
45         File JavaDoc dir = new File JavaDoc(directory);
46         if (dir.exists() && dir.isDirectory())
47         {
48             for (int i = 1; i <= numOfFiles; i++)
49             {
50                 FileWriter JavaDoc writer = null;
51                 try
52                 {
53                     File JavaDoc file = new File JavaDoc(directory + "/" + fileName + i + "." + fileExt);
54                     LOGGER.info("Writing file: " + file.getAbsolutePath());
55                     writer = new FileWriter JavaDoc(file);
56                     for (int l = 1; l <= linesPerFile; l++)
57                     {
58                         writer.write("This is a test file. blah.... blah.... blah....\n");
59                     }
60                 }
61                 catch (IOException JavaDoc ioe)
62                 {
63                     LOGGER.error("Error while writing file.", ioe);
64                 }
65                 finally
66                 {
67                     if (writer != null)
68                         try
69                         {
70                             writer.close();
71                         }
72                         catch (IOException JavaDoc e)
73                         {
74                             //Nothing we can do here
75
}
76                 }
77             }
78         }
79     }
80
81     public static void main(String JavaDoc[] args)
82     {
83         FileFlooder.flood(".", "file", "txt", 100, 1000);
84         System.out.println("done");
85     }
86
87 }
88
Popular Tags