Home > Blockchain >  How to print a file path in reverse order in Java?
How to print a file path in reverse order in Java?

Time:03-02

I want to print the relative path of file/folder in reverse order.

IPath projectRelativePath = element.getProjectRelativePath();

the result is "src/abc/foldername".

But I want the result in reverse manner ie. foldername/abc/src.

Can someone help me here? Is there any JAVA API available for this?

CodePudding user response:

I believe you could do that by splitting the path by "/" and afterwards concatenating the tokens in reverse order.

Here is a code snippet:

    Path path = Path.of("this/that/folder");
    List<String> tokens = Arrays.stream(path.toString().split("/")).toList();
    StringBuilder newPath = new StringBuilder();
    for(int i = tokens.size() - 1; i >= 0; i--){
       newPath.append(tokens.get(i));
       newPath.append("/");
    }
    System.out.println(newPath.toString());
  •  Tags:  
  • java
  • Related