KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > FileUtil


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2005 Marc Eppelmann
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21 package com.izforge.izpack.util;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32
33 /**
34  * Provides general global file utility methods
35  *
36  * @author marc.eppelmann
37  */

38 public class FileUtil
39 {
40   //~ Constructors ***********************************************************************
41

42   /**
43    * Creates a new FileUtil object.
44    */

45   public FileUtil(){}
46
47   //~ Methods ****************************************************************************
48

49   /**
50    * Gets the content from a File as StringArray List.
51    *
52    * @param fileName A file to read from.
53    *
54    * @return List of individual line of the specified file. List may be empty but not
55    * null.
56    *
57    * @throws IOException
58    */

59   public static ArrayList JavaDoc getFileContent( String JavaDoc fileName )
60                                   throws IOException JavaDoc
61   {
62     ArrayList JavaDoc result = new ArrayList JavaDoc();
63
64     File JavaDoc aFile = new File JavaDoc( fileName );
65
66     if( ! aFile.isFile() )
67     {
68       //throw new IOException( fileName + " is not a regular File" );
69
return result; // None
70
}
71
72     BufferedReader JavaDoc reader = null;
73
74     try
75     {
76       reader = new BufferedReader JavaDoc( new FileReader JavaDoc( aFile ) );
77     }
78     catch( FileNotFoundException JavaDoc e1 )
79     {
80       // TODO handle Exception
81
e1.printStackTrace();
82
83       return result;
84     }
85
86     String JavaDoc aLine = null;
87
88     while( ( aLine = reader.readLine() ) != null )
89     {
90       result.add( aLine + "\n" );
91     }
92
93     reader.close();
94
95     return result;
96   }
97
98   /**
99    * Searches case sensitively, and returns true if the given SearchString occurs in the
100    * first File with the given Filename.
101    *
102    * @param aFileName A files name
103    * @param aSearchString the string search for
104    *
105    * @return true if found in the file otherwise false
106    */

107   public static boolean fileContains( String JavaDoc aFileName, String JavaDoc aSearchString )
108   {
109     return ( fileContains( aFileName, aSearchString, false ) );
110   }
111
112   /**
113    * Tests if the given File contains the given Search String
114    *
115    * @param aFileName A files name
116    * @param aSearchString the String to search for
117    * @param caseInSensitiveSearch If false the Search is casesensitive
118    *
119    * @return true if found in the file otherwise false
120    */

121   public static boolean fileContains( String JavaDoc aFileName, String JavaDoc aSearchString,
122                                       boolean caseInSensitiveSearch )
123   {
124     boolean result = false;
125
126     String JavaDoc searchString = caseInSensitiveSearch
127             ? aSearchString.toLowerCase() : aSearchString;
128
129     ArrayList JavaDoc fileContent = new ArrayList JavaDoc();
130
131     try
132     {
133       fileContent = getFileContent( aFileName );
134     }
135     catch( IOException JavaDoc e )
136     {
137       // TODO handle Exception
138
e.printStackTrace( );
139     }
140
141     Iterator JavaDoc linesIter = fileContent.iterator( );
142
143     while( linesIter.hasNext() )
144     {
145       String JavaDoc currentline = (String JavaDoc) linesIter.next( );
146
147       if(caseInSensitiveSearch )
148       {
149         currentline = currentline.toLowerCase( );
150       }
151
152       if( currentline.indexOf( searchString ) > -1 )
153       {
154         result = true;
155
156         break;
157       }
158     }
159
160     return result;
161   }
162
163   /**
164    * Test main
165    *
166    * @param args
167    */

168   public static void main( String JavaDoc[] args ){}
169 }
170
Popular Tags