Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code: Shoudn't it be?or skip the getParentFile() call and do
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic