0

I need to construct a path which contains colon (:). Actually the path is not a folder/directory in windows/linux. It is just a path of JCR repo. Below code gives InvalidPathException when the path contains colon

import java.nio.file.Path;
import java.nio.file.Paths;

public class TestCLass {

    public static void main(String[] args) {
        final Path path = Paths.get("com", "repo:access","resource");
        //final Path path = Paths.get("com", "repoaccess","resource"); //Output - com/repoaccess/resource
        System.out.println(path);
    }
}

Exception

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 8: com\repo:access\resource
    at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
    at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
    at java.nio.file.Paths.get(Paths.java:84)

Is there any API to access colon(:) and any other special character?

ezhil
  • 977
  • 6
  • 15
  • 36
  • 1
    I don't understand the question. `:` is not a valid character in a `Path` as `Path` is intended to locate a file *on the filesystem*. What do you need ? – Arnaud Denoyelle Apr 05 '19 at 09:52
  • I want to construct the path which contains : Is there any way we manage this? – ezhil Apr 05 '19 at 09:54
  • where is the folder - in Windows or Linux ?? – Anish B. Apr 05 '19 at 09:54
  • 4
    Looks like you need [URL](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html) or [URI](https://docs.oracle.com/javase/8/docs/api/java/net/URI.html) instead. – Arnaud Denoyelle Apr 05 '19 at 09:55
  • 1
    Both windows & Linux. I want to construct a path like com/repo:access/resource – ezhil Apr 05 '19 at 09:58
  • @ezhil: Do have a look here:- https://stackoverflow.com/questions/41900064/opening-a-file-that-has-colons-in-the-filename-with-java-displayed-as-slashes-i – Abhinav Apr 05 '19 at 10:27

1 Answers1

0

As far as I know, neither Windows nor Linux allow ":" to be put in a file name. That's why Java throws InvalidPathException for such a path/file name. There's no API to work around a restriction imposed by the file system.

StackLloyd
  • 409
  • 2
  • 9
  • actually it is not folder name in windows/linux. It is path JCR repo. I am trying to construct this path for JCR repo – ezhil Apr 05 '19 at 13:44
  • Well then I'm sorry, but I've never used JCR. However, did you take a look at **javax.jcr**? I don't know where to point you at, but maybe you will find what you need [here](https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/index.html). – StackLloyd Apr 06 '19 at 14:09
  • Thanks for your input. I will have a look. – ezhil Apr 10 '19 at 03:41