Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting this InvalidPathException

Tags:

java

Have some problem with this code. Namely I am getting the message:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: [E:\Temp\564\324\123.txt]

public static void main(String[] args) throws Exception{
    Path sourseFile = Paths.get("E:\\Temp");
    Path[] result = searchFile(sourseFile, "123");
    for (Path path : result) {
        System.out.println(path);
    }        
}

public static Path[] searchFile (Path path, String fileName)throws Exception{

    DirectoryStream<Path> dirStream = Files.newDirectoryStream(path);
    ArrayList<Path> temp = new ArrayList<>();
    for (Path s : dirStream) {
        if (s.toFile().isDirectory()){
            temp.add(Paths.get(Arrays.toString(searchFile(s, fileName))));
        }
        else {
            if (s.toAbsolutePath().toString().contains(fileName)){
                temp.add(s.toAbsolutePath());
            }
        }
    }
    return temp.toArray(Path[]::new);
}

Full trace

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: [E:\Temp\564\324\123.txt] at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229) at java.base/java.nio.file.Path.of(Path.java:147) at java.base/java.nio.file.Paths.get(Paths.java:69) at s09.Task1.searchFile(Task1.java:28) at s09.Task1.searchFile(Task1.java:28) at s09.Task1.main(Task1.java:13)

like image 662
Demid Avatar asked Jun 24 '19 11:06

Demid


2 Answers

It appears that you're passing the string "[E:...]", including the surrounding square brackets added by Arrays.toString, to Paths.get. This then gets interpreted as a plain filename, in which the ":" character is indeed illegal.

like image 132
laszlok Avatar answered Nov 02 '22 18:11

laszlok


I think that the clue is in the exception message:

   Illegal char <:> at index 2: [E:\Temp\564\324\123.txt]

Notice that it is saying that the illegal character is at index 2.

Now Java uses zero-based indexing for strings and arrays and most other things1. So if the : character is at offset 2, then the [ character must be the first character of the pathname; i.e. the actual "pathname" is being formed from "[E:\Temp\564\324\123.txt]" including the square brackets.

That is wrong.

So where is that coming from?

 temp.add(Paths.get(Arrays.toString(searchFile(s, fileName))));

What?

You are calling Arrays.toString on some array, and expecting that to be a valid pathname. No way is that going to work. Read the javadocs for Arrays.toString().

For the record, here is a concise way to concatenate an array of strings to form a Path.

 String[] array = {"a", "b", "c"};
 Path p = Stream.of(array).map(Paths::get).reduce(Path::resolve).get();
 System.out.println(p);

1 - Including the offset in this exception message. I checked the source code.

like image 3
Stephen C Avatar answered Nov 02 '22 19:11

Stephen C