Hi,
I'm using the following code in my windows 7 machine. The list() method is not working.
It's showing messages: "Is a directory !!" and "Path: C:\Java Projects\src". But the fileList array's length is zero. It's printing "List array is empty !!" in the log file.
There are files and sub directories inside C:\Java Projects\src. What could be wrong?
import java.io.*;
public class FileSearch {
public static void main(
String[] args) {
try {
File dir = new File("C:\\Java Projects\\src");
if (!dir.isDirectory()) { System.out.println("Not a directory !!"); }
else { System.out.println("Is a directory !!"); }
System.out.println("Path: " + dir.getPath());
File logFile = new File("C:\\temp\\log.txt");
String[] fileList = dir.list();
PrintWriter pw = new PrintWriter(logFile);
if (fileList.length == 0) { for (String s : fileList) { pw.println(s); } }
else { pw.println("List array is empty !!"); }
pw.flush(); pw.close();
} catch (IOException ex) { System.out.println("IOException caught:- " + ex.toString()); }
}
}