Java 无法找到正确的文件路径

Java 无法找到正确的文件路径,java,selenium-webdriver,Java,Selenium Webdriver,我在修复正确的文件路径时遇到了一些问题 我有一个配置文件,下面是config.properties中的条目: strMasterTestSuiteFilePath="D:\\KeywordDrivenFramework\\MasterTestSuiteFile.xls" 然后我试着把这个属性解读为 Properties prop=new Properties(); prop.load("config.properties") String strpath=prop.getProperty("s

我在修复正确的文件路径时遇到了一些问题

我有一个配置文件,下面是config.properties中的条目:

strMasterTestSuiteFilePath="D:\\KeywordDrivenFramework\\MasterTestSuiteFile.xls"
然后我试着把这个属性解读为

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....")
为什么即使文件存在于path中,它也会阻塞else? 如何克服它? 我试过了
String str=strpath.replaceAll(“\”,“\\”)
//但是我得到了一些语法错误“字符串类型中的方法replaceAll(字符串,字符串)不适用于参数(字符串)” 有谁能帮我克服这个问题吗

找到我正在尝试的代码,哪里出了问题

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

我猜你想做的是:


String str=strpath.replaceAll(“\\\\”,“\\”)

\
是字符串中的特殊字符。要将其视为普通字符,请在其前面放置另一个
\
,因此
'\'
实际上是
'\\'


对于您的情况,我认为您希望使用
.replace()
而不是
.replaceAll()

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


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

String str=strpath.replaceAll(“\”,“\”)
将不起作用,因为您必须首先创建带有反斜杠(您不是)的
字符串,然后替换它们。还有一个愚蠢的问题,但文件是否确实存在?可能是,您使用的文件扩展名不正确。请检查您的文件的-
MasterTestSuiteFile
扩展名是否真的是
xls
?它可能是
xlsx
或其他东西。String str=strpath.replaceAll(“\\”,“\\\\”),当我尝试此语句时,我得到的是PatternSyntaxException这是因为
。replaceAll
需要一个正则表达式作为第一个参数。尝试使用
。替换
;)我尝试了String str=strMasterTSFilePath.replace('\','\\');它表示无效的字符常量。您应该尝试
String str=strpath.replace(“\\”,“\\\\”)
不要使用replaceAll(),使用replace()!该文件存在,唯一的问题是当我读取prop.getProperty(Sting)时,它返回一个“\”。使用此路径,如果我创建一个文件对象并使用exists()方法进行检查,它将无法正常工作。