If you are using Java 7 java.nio classes to read a file, you may come across the NoSuchFileException if the file is not found. NoSuchFileException is a checked exception that was added to Java 7 and it extends FileSystemException that in turn extends IOException.
Example:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
String fileName = "/Users/proj/src/main/sample.txt";
try (Stream<String> linesStream = Files.lines(Paths.get(fileName))) {
linesStream.forEach(System.out::println);
} catch (NoSuchFileException ex) {
System.out.println("File not found:" + fileName);
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Exception stack trace:
java.nio.file.NoSuchFileException: /Users/proj/src/main/sample.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at java.nio.file.Files.newBufferedReader(Files.java:2784)
at java.nio.file.Files.lines(Files.java:3744)
at java.nio.file.Files.lines(Files.java:3785)
at JavaReadTextFile.main(JavaReadTextFile.java:13)
Fix/Solutions:
- Make sure you have provided the absolute path of the file.
- Make sure the file exists.

-
Have Questions? Post them here!
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- How to uninstall/remove or disable Microsoft Teams - Teams
- Best way to calculate elapsed time in Java using Java 8 Duration & Instant Class with Nanoseconds precision - Java
- View in File Explorer option missing in SharePoint Online Edge browser - SharePoint
- Install Java 17 (LTS) JDK on ARM based M1/M2 Mac Natively - MacOS
- The package must have at least one . separator [Android Studio] - Android-Studio
- Resolve System.IO.PathTooLongException [Sharepoint C# .Net] - SharePoint
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years - Java
- How to Insert an Inverted Question Mark on Mac ¿ - MacOS