Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

List all files, directories and subdirectories that are in the path of the file selected.  RSS feed

 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks,

Can anyone help me out? I'm trying to List all files, directories and subdirectories that are in the path of a file selected on my desktop, and I'm trying to do it recursively, but I tried giving it a go myself but I really have no idea how to implement the write code or what to use besides the File, JFileChooser and arrays... can someone help me out a bit? i tried it before by using getparentfile to go up one dir for every recursive call but just got the files in the current directory listed along with a bunch of nullpointer exceptions
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I have this code.. it lists everything within the directory and it's subdirectories.. but i don't know what to do to list all the directories ect above my current dir..

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom jenkins wrote:So far I have this code.. it lists everything within the directory and it's subdirectories.. but i don't know what to do to list all the directories ect above my current dir..


Why do you need to list everything above your current directory? I though you just wanted to list all the files and subdirectories recursively from a given starting point.

Anyway, I can see you've found getParentFile(), so what specific problem are you having with it?
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's part of the requirement for an assignment I have..

I did find getParentfile(), and at the end of the method i tried to recursively call listFiles(dir.getParentFile()); to go up a directory.. i even tried getParentFile.getParentFile... ... well when i printed it out it did show the path i wanted but when i tried using it it didin't work...

.. i keep getting stack overflows and it can't get out of the current folder..

So I'm kind of stuck now trying to figure out what to do
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the exact wording of the assignment? It's not clear to me what you're trying to do. And your description of the errors is rather vague. You need to TellTheDetails(←click).
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment has us use JFileChooser to select a file and post the absolutefilename as well as the size of the file and lastly
we have to "List all files, directories and subdirectories that are in the path of the file selected." that's the part I'm stuck on.

We are supposed to use recursion to List all files, directories and subdirectories in the path of the file selected.. and i have no clue. I can only get the files and subdirectories in the current folder, but I don't know how to go to the parent directory of the current directory and list those items as well... i think i have to keep doing this until I reach the root file and finally list those folders/files and then the method is finished.

so pretty much i have to go from C:/ and list everything in the path of my selected file all the way down to the selected file itself.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom jenkins wrote:The assignment has us use JFileChooser to select a file and post the absolutefilename as well as the size of the file and lastly
we have to "List all files, directories and subdirectories that are in the path of the file selected." that's the part I'm stuck on.


That's kind of imprecisely worded, but nothing there suggests anything about parent directories to me.

Let's say we have the following structure:



If somebody chooses D.txt, possible reasonable interpretations are:

1. The chosen File object (which could be a file or directory) and everything below it. That would mean just D.txt here. Since it's a file, there's nothing below it.

2. Everything in the same directory as the chosen File object, and everything below that directory. In this case, D.txt is in directory C, so we'd list everything in and below C. This would mean D.txt, E, E/F.txt.

3. Like #1, but also list the parents of the chosen File object, up to the root. This would mean A, A/B, A/B/C, A/B/C/D.txt

4. Like #2, but also list the parents of the chosen File object, up to the root. This would mean A, A/B, A/B/C, A/B/C/D.txt, E, E/F.txt.

#2 makes the most sense to me. Nothing in your assignment suggests parent directories to me, but it's not very well worded, so in theory it could mean 3 or 4. HOWEVER, if you interpret the instructions in the sense of #3 or #4, it wouldn't make any sense to think that you're supposed to list those directories' subdirectories recursively as well. If you did, that would mean that whatever file you pick, you'd end up listing the entire directory structure of the whole file system.

You might want to try to get clarification from your instructor. Find out if you're supposed to list A and its ancestors as well, or just G.txt and everything in and below its directory.

So you need to figure out which one your instructor is talking about.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!