aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Usage of list() with File to search files and directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Usage of list() with File to search files and directories" Watch "Usage of list() with File to search files and directories" New topic
Author

Usage of list() with File to search files and directories

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
In Chapter 6, Sierra/Bates, discuss the java.io package and how it can be used to search files and directories, but I don't understand their explanation.

Below is my variation of their code, as I was trying to understand why it's not working:



Below is my output:

Something is null

What can possibly be null? There are other files in the same directory as Writer.java!

By the way, I also referenced the Java File API (http://java.sun.com/javase/6/docs/api/java/io/File.html), and I still don't understand.

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4244
    
    7

Well, here's the important line from the documentation:

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null.


In your example search is a File object representing a file, not a directory (you can also check this by looking at the value returned by search.isDirectory()). So it returns null.

If you use search.getParent(), that will return a File object representing the directory the file is in. Then call list() on that, and see what happens.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hi Matthew,

I modified the code such that it created a directory, and placed a file in the directory:




The output is:

found newFile.txt

However, I am having difficulty using getParent(). Below is modified (erroneous) code that uses getParent():



Below is the compiler error:

Writer.java:16: cannot find symbol
symbol : method list()
location: class java.lang.String
files = parent.list();
^
1 error



Please advise.




Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4244
    
    7

Ah, I was a bit careless there. getParent() returns a String, then you're trying to call list() on it, but String doesn't have a list() method.

You want getParentFile(), which returns a File object. Sorry about that.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I modified the code with getParentFile(). It compiles and runs, but still throws an exception, even though there are clearly things in the parent directory.



Output:

Something is null


How to fix this?


Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
In your code: Shoudn't it be?or skip the getParentFile() call and do
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hi,

I tried out the code, and it still reads contents from searchDir instead of the parent directory where the program is executing from. Even though the book hasn't mentioned this method, I'm still curious as to how it can work. Please see modified code below:





I appreciate your input
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I tried out the code, and it still reads contents from searchDir instead of the parent directory where the program is executing from.
You said searchDir. Did you mean searchThis? The code creates a directory then creates a file in that directory. Then it asks for the parent directory of the file you just created, which is the directory you just created. Then it prints the file names in that directory, which I assume is the file you just created. True? Please elaborate on what you think should happen.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I think your question of why your call to getParentFile() returned null has to do with absolute vs. relative paths. When you use an absolute path, the getParentFile() method returns a File instance. If you use a relative path, getParentFile() returns null. I guess when you use a relative path, File considers your path as the root.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Tom Reilly wrote:
I tried out the code, and it still reads contents from searchDir instead of the parent directory where the program is executing from.
You said searchDir. Did you mean searchThis? The code creates a directory then creates a file in that directory. Then it asks for the parent directory of the file you just created, which is the directory you just created. Then it prints the file names in that directory, which I assume is the file you just created. True? Please elaborate on what you think should happen.


Yes, I meant searchThis.

Here is what I think should happen:


Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
The newFile.getParentFile() returns the searchThis directory - not the parent of searchThis. If you want the parent of searchThis then you need to say search.getParentFile(). But this will return null. Please see my previous response about relative and absolute paths for the explanation.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I created some code snippits relating to getParent() and getFileParent() and it seems you actually have to create a directory within a directory in your code, otherwise it'll point to null. Will spend more time on this once I'm done with Sierra/Bates and knee-deep in mock tests and creating my own code for practice.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Usage of list() with File to search files and directories
 
Similar Threads
Reference Kathy Sierra chapter 6 doubt
Why not to enclose in try-catch block??
JAVA I/O
File's list() method and classpaths
Check folder and start program execution further