Uday
Uday

Reputation: 1484

unable to findright file path

I am facing some problem to fix the right file path.

I have a configuration file and following is the entry in config.properties:

strMasterTestSuiteFilePath="D:\\KeywordDrivenFramework\\MasterTestSuiteFile.xls"

Then i tried to read this property as

Properties prop=new Properties();
prop.load("config.properties")
String strpath=prop.getProperty("strMasterTestSuiteFilePath")
Syso(strpath)  //prints above path with single slash as D:\KeywordDrivenFramework\MasterTestSuiteFile.xls
//When i use the same var for File existance check it say not exists
File obj=new File(strpath)
if(obj.exists())
 Syso("Exists....")
else
 Syso("Does not exist....")

Why it is going to else block, even though the file exists at path? How to overcome it? I tried String str= strpath.replaceAll("\","\\") //but i am getting some syntax error "The method replaceAll(String, String) in the type String is not applicable for the arguments (String)" Can anyone help me how to overcome this?

Find the code which i am trying, where i am going wrong?

public void LoadMasterTestSuite()
    {
        String strGlobalConfigSettingFilePath=System.getProperty("user.dir")+"/src/GlobalConfigurationSettings.properties";
        FileInputStream objFIS; //Variable to hold FileSystem Objet
        File objFile;           //Variable to hold File Object

        try 
        {
            objFIS = new FileInputStream(new File(strGlobalConfigSettingFilePath));
            globalObjProp.load(objFIS);
            strMasterTSFilePath=globalObjProp.getProperty("strMasterTestSuiteFilePath");
            objAppLog.info("Master Test Suite File Path configured as "+strMasterTSFilePath);
        }catch (FileNotFoundException e) 
        {
            objAppLog.info("Unable to find Global Configuration Settings File. So aborting...");
            e.printStackTrace();
        }catch (IOException e) 
        {
            e.printStackTrace();
        }
        String str=strMasterTSFilePath.replace("\\", "\\\\");
        objFile=new File(str);
        System.out.println(str);
        if(objFile.exists())
        {
            LoadTestSuite(strMasterTSFilePath);
        }
        else
        {
            objAppLog.info("Master Test Suite File not found at Path: "+strMasterTSFilePath);
            System.exit(-1);
        }
    }

Upvotes: 0

Views: 101

Answers (2)

PKlumpp
PKlumpp

Reputation: 5233

I guess what you wanted to do was:

String str= strpath.replaceAll("\\\\","\\")

\ is a special character in a String. To Treat it like a normal character, put another \ in front of it, so '\' actually is '\\'.

And for your case, I think you want to use .replace() and not .replaceAll().

Upvotes: 1

EtherealSoul
EtherealSoul

Reputation: 11

When you debug, is strpath getting the correct path from the config file? I would also make sure that you're referencing the correct file path (paste the location into Windows Explorer and see if the file exists). If D:\ is a shared drive, use the actual server location.

Also, you can make sure that the extension of the Excel workbook is .xls and not .xlsx.

Upvotes: 0

Related Questions