001/*
002 * Created on 25.08.2005 *
003 */
004package com.izforge.izpack.util;
005
006import java.io.BufferedReader;
007import java.io.File;
008import java.io.FileNotFoundException;
009import java.io.FileReader;
010import java.io.IOException;
011
012import java.util.ArrayList;
013import java.util.Iterator;
014
015
016/**
017 * Provides general global file utility methods
018 *
019 * @author marc.eppelmann
020 */
021public class FileUtil
022{
023  //~ Constructors ***********************************************************************
024
025  /**
026   * Creates a new FileUtil object.
027   */
028  public FileUtil(){}
029
030  //~ Methods ****************************************************************************
031
032  /** 
033   * Gets the content from a File as StringArray List.
034   *
035   * @param fileName A file to read from.
036   *
037   * @return List of individual line of the specified file. List may be empty but not
038   *         null.
039   *
040   * @throws IOException
041   */
042  public static ArrayList getFileContent( String fileName )
043                                  throws IOException
044  {
045    ArrayList result = new ArrayList();
046
047    File      aFile = new File( fileName );
048
049    if( ! aFile.isFile() )
050    {
051      //throw new IOException( fileName + " is not a regular File" );
052      return result; // None
053    }
054
055    BufferedReader reader = null;
056
057    try
058    {
059      reader = new BufferedReader( new FileReader( aFile ) );
060    }
061    catch( FileNotFoundException e1 )
062    {
063      // TODO handle Exception
064      e1.printStackTrace();
065
066      return result;
067    }
068
069    String aLine = null;
070
071    while( ( aLine = reader.readLine() ) != null )
072    {
073      result.add( aLine + "\n" );
074    }
075
076    reader.close();
077
078    return result;
079  }
080
081  /** 
082   * Searches case sensitively, and returns true if the given SearchString occurs in the
083   * first File with the given Filename.
084   *
085   * @param aFileName A files name
086   * @param aSearchString the string search for
087   *
088   * @return true if found in the file otherwise false
089   */
090  public static boolean fileContains( String aFileName, String aSearchString )
091  {
092    return ( fileContains( aFileName, aSearchString, false ) );
093  }
094
095  /** 
096   * Tests if the given File contains the given Search String
097   *
098   * @param aFileName A files name
099   * @param aSearchString the String to search for
100   * @param caseInSensitiveSearch If false the Search is casesensitive
101   *
102   * @return true if found in the file otherwise false
103   */
104  public static boolean fileContains( String aFileName, String aSearchString,
105                                      boolean caseInSensitiveSearch )
106  {
107    boolean result = false;
108
109    String  searchString = new String( caseInSensitiveSearch
110                                       ? aSearchString.toLowerCase() : aSearchString );
111
112    ArrayList fileContent = new ArrayList();
113
114    try
115    {
116      fileContent = getFileContent( aFileName );
117    }
118    catch( IOException e )
119    {
120      // TODO handle Exception
121      e.printStackTrace(  );
122    }
123
124    Iterator linesIter = fileContent.iterator(  );
125
126    while( linesIter.hasNext() )
127    {
128      String currentline = (String) linesIter.next(  );
129
130      if( caseInSensitiveSearch == true )
131      {
132        currentline = currentline.toLowerCase(  );
133      }
134
135      if( currentline.indexOf( searchString ) > -1 )
136      {
137        result = true;
138
139        break;
140      }
141    }
142
143    return result;
144  }
145
146  /** 
147   * Test main
148   *
149   * @param args
150   */
151  public static void main( String[] args ){}
152}