0

I have problems when looking for the path, the extension does bring it to me, but since I have a space on my local disk, it has a space and that is generating errors like I did to consider it, this is my code!

--> I have problems with having the local disk with a spacing c:/Some/Folder withpaces/Or/Other.pdf

public class Exame{
    public static void main(String[] args) throws URISyntaxException {

        System.out.println(getRoute("c:/Some/Folder withpaces/Or/Other.pdf","xlsx"));
    }

    public static String getRoute(String router,String newExtension) throws URISyntaxException {

        URI uri = new URI(router);
        String path = uri.getPath();

        String lastSegment = path.substring(path.lastIndexOf('/') + 1);
        String newName=lastSegment.substring(0,lastSegment.lastIndexOf("."))+"."+newExtension;

        return new File(path.substring(0,path.lastIndexOf('/')+1),newName).getPath();
    }
}
Swagerich
  • 1
  • 3
  • 1
    Spacing isn't the problem. Windows uses double backslashes for file paths. You should also use `Path` objects, not string methods/objects – OneCricketeer May 13 '22 at 01:25
  • Also see [How to change file extension](https://stackoverflow.com/questions/12209801/how-to-change-file-extension-at-runtime-in-java) – OneCricketeer May 13 '22 at 01:27
  • Please edit your question to include the full error, it will typically explain exactly what is wrong, otherwise we can only guess. – sorifiend May 13 '22 at 01:51

0 Answers0