2014-07-17 65 views
0

我面临一些问题来修复正确的文件路径。无法找到正确的文件路径

我有一个配置文件,以下是在config.properties条目:

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

然后我试图为什么它要else块读取此属性为

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....") 

,甚至虽然文件存在路径? 如何克服它? 我试过 String str= strpath.replaceAll("\","\\") //但我得到一些语法错误“String类型的方法replaceAll(String,String)不适用于参数(String)” 任何人都可以帮助我解决这个问题吗?

找到我正努力,我要去的地方错误的代码?

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); 
     } 
    } 
+0

'字符串str = strpath.replaceAll(“\”,“\\”)'将无法工作,因为你必须要能够创建'String'用反斜杠(你是不是)在第一个地方,然后替换他们。而一个愚蠢的问题,但文件确实存在? – Eypros

+0

可能是,您使用的文件扩展名不正确。你能否检查一下你的文件是否是'MasterTestSuiteFile'扩展名'xls'?它可能是'xlsx'或其他东西。 – TDHM

回答

1

我猜测你想要做的是:

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

\是一个String一个特殊字符。为了像正常字符那样对待它,在其前面放置另一个\,所以'\'实际上是'\\'

而对于你的情况,我想你想使用.replace(),而不是.replaceAll()

+0

字符串str = strpath.replaceAll(“\\”,“\\\\”)//当我尝试这种说法我得到PatternSyntaxException – Uday

+0

这是因为'.replaceAll'需要一个正则表达式作为第一个参数。尝试使用'.replace'来代替;) – PKlumpp

+0

我试过String str = strMasterTSFilePath.replace('\','\\');它说无效的字符常量。 – Uday

0

当你调试时,strpath是从配置文件中获取正确的路径吗?我也会确保你引用了正确的文件路径(将位置粘贴到Windows资源管理器中并查看文件是否存在)。如果D:\是共享驱动器,请使用实际的服务器位置。

此外,还可以确保Excel工作簿的扩展名是.xls和不是原来的.xlsx。

+0

该文件存在,唯一的问题是当我读prop.getProperty(Sting)它返回与单个“\”。有了这个路径,如果我创建一个文件对象,并检查使用exists()方法它不能罚款。 – Uday

相关问题