Wednesday, February 20, 2013

Selenium : Program on File operations and String operations as 2 diffrent classes called in main class

Program on File operations and String operations as 2 diffrent classes called in main class




import java.io.File;
import java.io.IOException;
import java.util.Locale;
import javax.swing.JOptionPane;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.write.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class File_StringCheck
{
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException
{
String sDir = JOptionPane.showInputDialog("Enter any Directory");
cFile_Operations oFile_Operations=new cFile_Operations();

System.out.println(oFile_Operations.mCreate_New_Excel(sDir, "Sheet1"));
JOptionPane.showMessageDialog(null, "over");
}
}



class cCheck_String
{
static String sRes="";
static int i;

public cCheck_String( )
{
// TODO Auto-generated constructor stub
}



//aArr - is an string array type ,sPattern - any pattern of regular expression
//Returns - index of array elements matching in the form of string seperated by comma eg-2,3,4
public static String cCheck_Pattern_Array(String aArr[] , String sPattern)
{


Pattern oPattern = Pattern.compile(sPattern);
for (i=0 ; i <= aArr.length-1 ; i++ )
{
Matcher oMatcher = oPattern.matcher(aArr[i]);
if (oMatcher.find( )== true)
{
System.out.println(aArr[i]+"--matches");
sRes=sRes+","+i;
}
}
return sRes;
}

//sStr - is an string type ,sPattern - any pattern of regular expression
//returns true if matching else returns false
public boolean cString_match(String sStr, String sPattern)
{
Pattern oPattern = Pattern.compile(sPattern);
Matcher oMatcher = oPattern.matcher(sStr);
if (oMatcher.find( )== true)
{
System.out.println(sStr+"--matches");
return true;
}
else
{
System.out.println(sStr +"--no matches");
return false;
}
}

}



class cFile_Operations
{
public static boolean mFile_Exsistence(String sPath)
{
File oFile=new File(sPath);
if (oFile.exists()== true )
{
System.out.println("file exist--mFile_Exsistence");
return true;
}
else
{
System.out.println("file does not exist--mFile_Exsistence ");
return false;
}
}

public static boolean mFile_delete(String sFull_path)
{
if (mFile_Exsistence(sFull_path)== true )
{
File oFile=new File(sFull_path);
oFile.delete();
System.out.println("File Deleted--mFile_delete");
return true;
}
else
{
System.out.println("File does not exsist--mFile_delete");
return false;
}
}
//sFull_path="D:\Selenium\test\Excel.xls"
public static boolean mCreate_New_Excel(String sFull_path,String sSheet_Name) throws IOException, WriteException
{
if (mFile_Exsistence(sFull_path)== false )
{
WritableWorkbook workbook = Workbook.createWorkbook(new File(sFull_path));
workbook.createSheet(sSheet_Name, 0);
workbook.close();
System.out.println("Excel file created--mCreate_New_Excel");
return true;
}
else
{
System.out.println("Excel file not created--mCreate_New_Excel");
return false;
}
}

public static String[] mFiles_in_directory(String sDirectory_Path)
{
File oDir = new File(sDirectory_Path);
String[] aFiles = oDir.list();
return aFiles;
}
}


No comments:

Post a Comment