Описание тега file-not-found
An error, exit status, or exception that indicates that the file denoted by a specified pathname could not be found.
An error, exit status, or exception that indicates that the file denoted by a specified pathname could not be found. In other words, you're trying to access a file that doesn't exist where the code is expecting it to be.
This could occur as a result of many different actions, such as...
- The file doesn't actually exist, or was deleted before you could access it
- There is a spelling mistake in your pathname, or the pathname contains characters that aren't permitted in the pathname.
- You're using a relative pathname (such as
file.txt
) and the file doesn't exist in the default working directory for your application - The code is getting confused due to different directory path indicators on different operating systems (ie
/
vs\
) - The programming language interprets the
\
character in a String to be a escape character for a special symbol (such as\n
indicating new-line), and you're using single\
slashes as directory indicators instead of\\
for a single escaped slash. - In some languages, it could also give this exception if you aren't able to access the file due to security permissions.
Your code should be able to prevent or handle this condition, such as by doing the following...
- Detect - Many programming languages allow you to create a
File
object as a virtual representation of a physical file. Further to this, you can usually call a method or function that asks whether the file exists, such asexists();
. This would allow you to check the file exists before you attempt to access it - Prevent - If the user is selecting a file, present them with a
FileChooser
dialog rather than a text entry field.FileChooser
dialogs echo the files that exist in the system, so spelling mistakes are avoided. If you're using relative paths, convert them to absolute paths if practical, or ensure you set the relative path location to a known directory before trying to access files relatively. - Handle - Wrap your file access methods in exception-handling code, such as a try-catch block. This will allow you to catch unexpected exceptions, and perform an alternate operation, such as alerting the user of the error.