Skip to main content

Working with File Path Separators

Something I learned very recently. 


Have you ever written code which involved file path's? If yes then this post might be helpful...Even otherwise knowing something unknown or brushing up concepts known won't hurt - would they? :)


So here I go, I have my Selenium Scripts which might run on both Linux and Windows machines. I don't store my test files on a central location which can be accessed over the network/internet, so when my script runs on a Linux machine it would look locally on the linux file system for the test files. Same is the case when I run my tests on Windows. 


Here is what I used to do :) 


My Initial Approach: 


If ( Test_Running_On_Linux){

return System.getProperty("user.dir") + "/" + TestProperties.strFilePathPart1 + "/"+ TestProperties.strFilePathPart2;

}
else if(Test_Running_On_Windows){

return System.getProperty("user.dir") + "\\" + TestProperties.strFilePathPart1 + "\\"+ TestProperties.strFilePathPart2;

}


Now what if while forming that file path, I miss my slashes -- my whole test fails for a file separator? Not fair! 


So here is a better approach: 



If ( Test_Running_On_Linux){

return System.getProperty("user.dir") + File.separator + TestProperties.strFilePathPart1 + File.separator + TestProperties.strFilePathPart2;

}
else if(Test_Running_On_Windows){

return System.getProperty("user.dir") + File.separator + TestProperties.strFilePathPart1 + File.separator + TestProperties.strFilePathPart2;

}


Thus we handle file separators better!
For more information about File Object and Properties: Java File ObjectJava System Properties Tutorial 

Comments

Popular posts from this blog

wget error–“zsh: parse error near &”

There is no doubt that I prefer wget way over any other type of downloads… Syntax: wget <DOWNLOAD_URL>   If you get this error “ zsh: parse error near & ” then its probably because your download URL has a “&” so you should try giving your DOWNLOAD_URL in double quotes wget “<DOWNLOAD_URL>”   If you are trying to download from a site which needs you to give your credentials then you can try giving it this way wget --http-user=<UserName> --http-password=<Password> “<DOWNLOAD_URL>”   Hope this helps

How to Unpack a tar file on Windows?

On Windows: You can download a simple command line tool to do this. You can download the tool from here Usage can be found on the website but pasting it here too for convenience: C:\>TarTool.exe Usage : C:\>TarTool.exe sourceFile destinationDirectory C:\>TarTool.exe D:\sample.tar.gz ./ C:\>TarTool.exe sample.tgz temp C:\>TarTool.exe -x sample.tar temp TarTool 2.0 Beta supports bzip2 decompression for files with extensions like tar.bz2 and .bz2 . TarTool -xj sample.tar.bz2 temp or TarTool -j sample.bz2 Download TarTool 2.0 Beta from here Unpack a .txz file on Windows Use the 7zip tool  to unpack a .txz file on windows On Linux: You can use the bzip2 and tar combined to do this… for ex: bzip2 –cd <tar.bz_fileName> | tar –xvf - This will unpack the contents of the tar.bz file Happy Un-Tar-ing

Apache Commons StringUtils.isEmpty() vs Java String.isEmpty()

You might want to test for if a String is empty many a times. Before we jump onto the numerous solutions available let us take a look at how we define “Empty String”   The difference in the two methods given by Apache and Java are dependent on how we define an empty string. Java String.isEmpty returns a boolean true if the string’s length is zero. If the string has null it throws NullPointerException Apache StringUtils.isEmpty returns a boolean true if the string is either null or has length is zero   Thus its purely dependent on how you are defining “empty string” in your program which will decide which function to use…BTW if you want to skip using Apache Commons funciton and would want to stick to java then you can have your own function like this:   public static boolean isEmptyOrNull(String strStringToTest) {                  return strStringToTest == null || strStringToTest.trim().isEmpty(); }