Reason 1
The file does not exist really. Although you can see the file, the path you may have entered may be incorrect. For example, the following code may throw exception in some operating systems:File f=new File("D:\myfile.txt");
This is because the file separator character is '\' in windows os. since \ is used to display non graphical characters like '\n' you should use its corresponding escape sequence. So "D:\\myfile.txt" would work on windows. If your java application is a cross platform program, you should consider using File.separator instead of '\\' because not all operating systems use '\\' as file separator character. So, the best approach is ;
File f=new File("D:"+File.separator+"myfile.txt");
File.separator is the system-dependent default name-separator character, represented as a string for convenience. This string contains a single character, namely File.separatorChar. If you want to get this character as a Character itself, you can use File.separatorChar. There can be other mistakes in the path.
No comments:
Post a Comment