Exception in thread main java.nio.file.NoSuchFileException

Exception Stack:
Exception in thread "main" java.nio.file.NoSuchFileException: /Users/c2c/lambdas/sample-file.txt
	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
	at java.base/sun.nio.fs.UnixFileSystemProvider.newFileChannel(UnixFileSystemProvider.java:182)
	at java.base/java.nio.channels.FileChannel.open(FileChannel.java:292)
	at java.base/java.nio.channels.FileChannel.open(FileChannel.java:345)
	at java.base/java.nio.file.Files.lines(Files.java:4038)
	at java.base/java.nio.file.Files.lines(Files.java:4130)
	at JavaFilesWithStreamExample.main(JavaFilesWithStreamExample.java:19)
Reason for NoSuchFileException exception:

The reason for "no such file exception" is either the file that you are trying to process is either missing, misspelled or an incorrect path is provided for the file.

Fix NoSuchFileException exception:

Make sure that,

  • Print the path in the code and check if it is really present,
  • File name provided is correct (check file extension as well)
  • File path (relative/absolute) is provided correctly
  • Check the case of the file name/path
Try Out Code2care Dev Tools:


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.