Use Relative path in place of absolute path

2019-09-05 04:27发布

问题:

First of all i request people do not consider it as a duplicate question, please look into query. I am copying the xml files from one folder to other folder, in the source folder, i have some files which have some content like "backingFile="$IDP_ROOT/metadata/iPAU-SP-metadata.xml" but while writing to the destination folder. i am replacing the "$IDP_ROOT" with my current working directory. The entire copying of files is for deploying into tomcat server. The copying is done only when server starts for the first time. Problem: If i change the folder name from my root path in my machine after i run the server, the entire process will be stopped because the destination folder files already contains the content which is with existed files names or folder names.

So i want to change it to relative path instead absolute path. What is the best way to do it?

Please look at code below:

        // Getting the current working directory
        String currentdir = new File(".").getAbsoluteFile().getParent() + File.separator;

if(currentdir.indexOf("ControlPanel")!=-1){
    rootPath=currentdir.substring(0, currentdir.indexOf("ControlPanel"));
}else{
    rootPath=currentdir;            
}

rootPath = rootPath.replace("\\", "/"); 



        // target file in source folder is having "backingFile="$IDP_ROOT/metadata/iPAU-SP-metadata.xml"

String content = FileReaderUtil.readFile(targetFile,
        rootPath + "Idp/conf");
content = updatePath(content, Install.rootPath
        + "IdP/IdPserver/metadata","$IDP_ROOT");
FileWriterUtil.writeToFile(Install.rootPath
        + "IdP/IdPserver/idp/conf", content,
        targetFile);

    //  update method
   public String updatePath(String content, String replaceString,String replaceKey) {
replaceKey = replaceKey!=null ? replaceKey : "$IDP_SERVER_ROOT";
replaceString= replaceString.replace("\\","/");
String updateContent = content.replace(replaceKey,
        replaceString);
return updateContent;

}