简体   繁体   中英

Create a function for File.separator using Pattern Matcher

A static analysis tool when running on my java project gives "Portability Flaw In File Separator" error and I need to fix it. In my code, I have fileUnsafe. I want to use a method to convert it into fileSafe (explained below).

// Case 1
//no platform independence, good for Unix systems
File fileUnsafe = new File("tmp/abc.txt");

//platform independent and safe to use across Unix and Windows
File fileSafe = new File("tmp"+File.separator+"abc.txt");

Similarly for paths like -

// Case 2
//no platform independence, good for Unix systems
File fileUnsafe = new File("/tmp/abc.txt");

// platform independent and safe to use across Unix and Windows
File fileSafe = new File(File.separator+"tmp"+File.separator+"abc.txt");

I have multiple of these file addresses throughout my project and I need to create some conversion method that could just take in this path as a string, append File.separator to it, and return it. Something like this -

File fileSafe = new File(someConversionMethod("/tmp/abc.txt"));

I tried this method but it gives me NullPointerException on case 2.

public static String someConversionMethod(String target) {
        Pattern ptr = Pattern.compile("[\\\\\\\\|/]+");
        Matcher mtr = ptr.matcher(target);
        return mtr.replaceAll(File.separator + "" + File.separator);
    }

Any help either fixing this method or suggesting a graceful way to handle this situation would be appreciated.

nit - I referred to Replacing character with File.separator using java.regex Pattern Matcher but it didn't really help my case.

I would try splitting the string at the file separator into an array like so.

String str = "/tmp/abc.txt";
String result = "";
String rgx = "\\\\|/";
String [] arrOfStr = str.split(rgx);;

Then you can add your File.separator back in using a for loop. Like this:

    for (int i = 1; i < arrOfStr.length ; i++)
        result += File.separator + arrOfStr[i];

I start from index one because the first slash gets doubled in the resulting string.

Since this is a one time change, you could use the regex find and replace in Eclipse

For the first case: use Regex: ^File\\sfileUnsafe\\s=\\snew File\\(\\"(?<folder1>[^\\/]+)\\/(?<fileName>[^\\.]+)(?<extension>\\.\\w{3})\\"\\);

Replace with: File fileSafe = new File("${folder1}"+File.separator+"${fileName}${extension}");

Demo

For the second case: use Regex: ^File\\sfileUnsafe\\s=\\snew File\\(\\"\\/(?<folder1>[^\\/]+)\\/(?<fileName>[^\\.]+)(?<extension>\\.\\w{3})\\"\\);

Replace with: File fileSafe = new File(File.separator+"${folder1}"+File.separator+"${fileName}${extension}");

Demo

if you have more than one folder, you could continue this pattern and fix them.

I admit, this is not a clean straight forward way, but will get the job done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM