I have a small code wich can return the list of files under any directory.
What I need to do is get the Directories and Files under the first given directory.
This is the code I’m using.
File dir = new File("C:/myDocument/myFolder");
String[] children = dir.list();
if (children == null) {
} else {
for (int i=0; i<children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
Another thing is when I select the path from Windows 7, I get this C:\myFolder\myFolder.
If I use this path in Java I get this error Invalide Escape sequence
Do I have to change it to C:/myDocument/myFolder to get it work.
Help.
Thanks
Yes, forward slashes are fine. They get normalized to the OS-dependent separator.
What the error tells you is that
\mis an invalid escape sequence. Each backward slash tries to escape the following character. So if you need backward slashes in a string, use a double slash:"c:\\myDocuments\\myFolder"In order to get directories and files, you use
.listFiles()and thenfile.isDirectory()to check if it’s a directory.