Java File.separator example.

Different operating systems use different characters as file and path separators. When our application has to run on multiple platforms, we need to handle these correctly.
To handle this requirement we can use Java provide File.separator.

This is an example of used / at start and the end .

String destination = System.getProperty(“user.dir”) + “/ExtentReport/” + “/Screenshots/” + result.getMethod().getMethodName() + dateName + “.png”;

So instead of using / sign for path separators we could use File.separator

import java.io.File;String destination = System.getProperty(“user.dir”) + File.separator + “ExtentReport” + File.separator +”Screenshots” + File.separator + result.getMethod().getMethodName() + dateName + “.png”;
To use it you will have to add this import
Example in code

Another example is instead of

File jsonBody = new File(System.getProperty("user.dir") + "//testdata_files//TC-02//" + "CreateIncident.json");

I used

File jsonBody = new File(System.getProperty("user.dir") +File.separator + "testdata_files"+File.separator +"TC-02" +File.separator + "CreateIncident.json");

No responses yet