[Solved] InvalidPathException: Illegal char at index XYZ

The java.nio.file.InvalidPathException is a Runtime and Unchecked Exception which occured while running the program when file string path cannot be converted to Path. There can be multiple reason of throwing this exception as below:

  • String path contains invalid characters..
  • String path is invalid for other file system specific reasons (Ex: windows use (\) while linux use(/))

Note: When this issue occured it shows the string path index also where this path is invalid.

InvalidPathException is sub class of IllegalArgumentException

public class InvalidPathException extends IllegalArgumentException

Invalid Path Example

On Windows-based platforms, invalid path characters might include ASCII/Unicode characters 1 through 31, as well as pipe (|),null (\0) tab(\t) or It may contain ‘?’ , or ‘:’ or * in the wrong place.

InvalidPathException Example

When you execute this program over the Windows OS machine will through InvalidPathException because In Windows the directory path always separated by double slash (\) however in this program the last directory separator is having single slash(\) file name (test.txt) this will make the special character as ‘\t’ which is consider as tab character.
Due to this JVM will consider this string path as invalid and throw InvalidPathException.

public class InvalidPathExceptionExample
	{
	public static void main(String[] args) {
		try {
			List<String> allLines = Files.readAllLines(Paths.get("C:\\Users\\facingissuesonit\\Desktop\\logs\test.txt"));

			for (String line : allLines) {
				if(!line.contains("SAFE"))
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	}
	

Output

	Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <	> at index 60: C:\Users\facingissuesonit\Desktop\logging   est.txt
	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)
	at InvalidPathExceptionExample.main(ReadLineByLine.java:10

Solution

To resolve this issue in above path, place the double slash (\) after ‘logging’ as below.

Example : C:\\Users\\facingissuesonit\\Desktop\\logs\\test.txt

Recommendations

We also have some recommendations to resolve similar issues in other places:

  • Always use the path in windows with double slash (\) and linux with backslash (/). In case of path is dynamic and not sure about the OS for deploy of applciations always use File.separator which will convert to accounding to OS.
  • When this exception occured it always show the Index of invalid character in String path that will help you to remove the invalid character from path.

Conclusion

In this post you learn about the InvalidPathException and it’s constructor. Also explained one example of InvalidPathException with the solutions of resolving this issue in your code.

Related Posts

Your Feedback Motivate Us

If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

Happy Learning !!!