001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2005 Marc Eppelmann
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package com.izforge.izpack.util;
022:
023: import java.io.BufferedReader;
024: import java.io.File;
025: import java.io.FileNotFoundException;
026: import java.io.FileReader;
027: import java.io.FilenameFilter;
028: import java.io.IOException;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032:
033: import java.net.URL;
034:
035: /**
036: * Provides general global file utility methods
037: *
038: * @author marc.eppelmann
039: */
040: public class FileUtil {
041: //~ Constructors ***********************************************************************
042:
043: /**
044: * Creates a new FileUtil object.
045: */
046: public FileUtil() {
047: }
048:
049: //~ Methods ****************************************************************************
050:
051: /**
052: * Gets the content from a File as StringArray List.
053: *
054: * @param fileName A file to read from.
055: *
056: * @return List of individual line of the specified file. List may be empty but not
057: * null.
058: *
059: * @throws IOException
060: */
061: public static ArrayList getFileContent(String fileName)
062: throws IOException {
063: ArrayList result = new ArrayList();
064:
065: File aFile = new File(fileName);
066:
067: if (!aFile.isFile()) {
068: //throw new IOException( fileName + " is not a regular File" );
069: return result; // None
070: }
071:
072: BufferedReader reader = null;
073:
074: try {
075: reader = new BufferedReader(new FileReader(aFile));
076: } catch (FileNotFoundException e1) {
077: // TODO handle Exception
078: e1.printStackTrace();
079:
080: return result;
081: }
082:
083: String aLine = null;
084:
085: while ((aLine = reader.readLine()) != null) {
086: result.add(aLine + "\n");
087: }
088:
089: reader.close();
090:
091: return result;
092: }
093:
094: /**
095: * Searches case sensitively, and returns true if the given SearchString occurs in the
096: * first File with the given Filename.
097: *
098: * @param aFileName A files name
099: * @param aSearchString the string search for
100: *
101: * @return true if found in the file otherwise false
102: */
103: public static boolean fileContains(String aFileName,
104: String aSearchString) {
105: return (fileContains(aFileName, aSearchString, false));
106: }
107:
108: /**
109: * Tests if the given File contains the given Search String
110: *
111: * @param aFileName A files name
112: * @param aSearchString the String to search for
113: * @param caseInSensitiveSearch If false the Search is casesensitive
114: *
115: * @return true if found in the file otherwise false
116: */
117: public static boolean fileContains(String aFileName,
118: String aSearchString, boolean caseInSensitiveSearch) {
119: boolean result = false;
120:
121: String searchString = caseInSensitiveSearch ? aSearchString
122: .toLowerCase() : aSearchString;
123:
124: ArrayList fileContent = new ArrayList();
125:
126: try {
127: fileContent = getFileContent(aFileName);
128: } catch (IOException e) {
129: // TODO handle Exception
130: e.printStackTrace();
131: }
132:
133: Iterator linesIter = fileContent.iterator();
134:
135: while (linesIter.hasNext()) {
136: String currentline = (String) linesIter.next();
137:
138: if (caseInSensitiveSearch) {
139: currentline = currentline.toLowerCase();
140: }
141:
142: if (currentline.indexOf(searchString) > -1) {
143: result = true;
144:
145: break;
146: }
147: }
148:
149: return result;
150: }
151:
152: /**
153: * Gets file date and time.
154: *
155: * @param url The URL of the file for which date and time will be returned.
156: * @return Returns long value which is the date and time of the file. If any error
157: * occures returns -1 (=no file date and time available).
158: */
159: public static long getFileDateTime(URL url) {
160: if (url == null)
161: return -1;
162:
163: String fileName = url.getFile();
164: if (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\')
165: fileName = fileName.substring(1, fileName.length());
166:
167: try {
168: File file = new File(fileName);
169: // File name must be a file or a directory.
170: if (!file.isDirectory() && !file.isFile()) {
171: return -1;
172: }
173:
174: return file.lastModified();
175: } catch (java.lang.Exception e) { // Trap all Exception based exceptions and return -1.
176: return -1;
177: }
178: }
179:
180: public static String[] getFileNames(String dirPath)
181: throws Exception {
182: return getFileNames(dirPath, null);
183: }
184:
185: public static String[] getFileNames(String dirPath,
186: FilenameFilter fileNameFilter) throws Exception {
187: String fileNames[] = null;
188: File dir = new File(dirPath);
189: if (dir.isDirectory()) {
190: if (fileNameFilter != null) {
191: fileNames = dir.list(fileNameFilter);
192: } else {
193: fileNames = dir.list();
194: }
195: }
196: return fileNames;
197: }
198:
199: /**
200: * Test main
201: *
202: * @param args
203: */
204: public static void main(String[] args) {
205: }
206: }
|