• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java file I/O

 
Philip Mat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()); }


}

}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic you coded is essentially this:

Do you see the problem?

 
Philip Mat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... That was a stupid mistake from my side. Thanks a lot for replying...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!