• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java file I/O

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()); }


}

}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops... That was a stupid mistake from my side. Thanks a lot for replying...
reply
    Bookmark Topic Watch Topic
  • New Topic