1 21 package com.izforge.izpack.util; 22 23 import java.io.BufferedReader ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileReader ; 27 import java.io.IOException ; 28 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 32 33 38 public class FileUtil 39 { 40 42 45 public FileUtil(){} 46 47 49 59 public static ArrayList getFileContent( String fileName ) 60 throws IOException 61 { 62 ArrayList result = new ArrayList (); 63 64 File aFile = new File ( fileName ); 65 66 if( ! aFile.isFile() ) 67 { 68 return result; } 71 72 BufferedReader reader = null; 73 74 try 75 { 76 reader = new BufferedReader ( new FileReader ( aFile ) ); 77 } 78 catch( FileNotFoundException e1 ) 79 { 80 e1.printStackTrace(); 82 83 return result; 84 } 85 86 String 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 107 public static boolean fileContains( String aFileName, String aSearchString ) 108 { 109 return ( fileContains( aFileName, aSearchString, false ) ); 110 } 111 112 121 public static boolean fileContains( String aFileName, String aSearchString, 122 boolean caseInSensitiveSearch ) 123 { 124 boolean result = false; 125 126 String searchString = caseInSensitiveSearch 127 ? aSearchString.toLowerCase() : aSearchString; 128 129 ArrayList fileContent = new ArrayList (); 130 131 try 132 { 133 fileContent = getFileContent( aFileName ); 134 } 135 catch( IOException e ) 136 { 137 e.printStackTrace( ); 139 } 140 141 Iterator linesIter = fileContent.iterator( ); 142 143 while( linesIter.hasNext() ) 144 { 145 String currentline = (String ) 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 168 public static void main( String [] args ){} 169 } 170 | Popular Tags |