KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > ant > Util


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2006 John Lewis
5  * Copyright (C) 2006 Mark Doliner
6  *
7  * Note: This file is dual licensed under the GPL and the Apache
8  * Source License 1.1 (so that it can be used from both the main
9  * Cobertura classes and the ant tasks).
10  *
11  * Cobertura is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published
13  * by the Free Software Foundation; either version 2 of the License,
14  * or (at your option) any later version.
15  *
16  * Cobertura is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with Cobertura; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24  * USA
25  */

26
27 package net.sourceforge.cobertura.ant;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintStream JavaDoc;
36
37 class Util
38 {
39
40     static File JavaDoc createTemporaryTextFile(String JavaDoc prefix) throws IOException JavaDoc
41     {
42         File JavaDoc outputFile;
43         outputFile = File.createTempFile(prefix, ".txt");
44         outputFile.deleteOnExit();
45         return outputFile;
46     }
47
48     /**
49      * Returns the text of a file as a string.
50      *
51      * @param file The file to read.
52      * @return A string containing the text of the file
53      */

54     static String JavaDoc getText(File JavaDoc file) throws FileNotFoundException JavaDoc, IOException JavaDoc
55     {
56         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
57         PrintStream JavaDoc ps = new PrintStream JavaDoc(baos);
58         BufferedReader JavaDoc reader = null;
59         try
60         {
61             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
62             String JavaDoc line;
63             while ((line = reader.readLine()) != null)
64             {
65                 ps.println(line);
66             }
67             ps.close();
68         }
69         finally
70         {
71             ps.close();
72             if (reader != null)
73             {
74                 try
75                 {
76                     reader.close();
77                 }
78                 catch (IOException JavaDoc e)
79                 {
80                     System.err.println("IOException when closing file " + file.getAbsolutePath());
81                     e.printStackTrace();
82                 }
83             }
84         }
85
86         return baos.toString();
87     }
88
89 }
90
Popular Tags