• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Getting a Directory listing (Java:16.0.1, IDE: Ecli[se, OS:Win 10)

 
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this should have been posted in the beginers fourm...   How do I get a directory listing?  I've tried several programs posted on the web, and none of them work...  Several just give me a generic IOException.

I would like to display the contents of d:\Data  (which contains one sub-folder: "More Data" and two files: "test.txt" & "test2.txt").

Any help would be appreciated.
~d


 
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the API of the Files class. There are the methods list and walk
 
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Share what you tried and we can help sort out what went wrong.

Here's a simple example:
Output:
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am such a noob...  I needed "import java.io.*;"  It works now.  Thanks.



 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cleaned up code (found on web, but they were trying to do way more than I needed):

 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to try  your code, Ron... I like it.   Thanks again!
~d
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks!




 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need these two imports:
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you are not familiar with Java Streams (added in Java 8) ...

This:
Is equivalent to this:
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Man there is so much to learn.  Most books (that I have) talk about Java 8.  We're on 16, now...  Are there any newer books you recommend?  I've been reading, Java(tm) A Beginner's Guide Sixth Edition, by Herbert Schildt.  I'm up to chapter 11.
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am probably not the best one to recommend general Java books, but I'll add this thread the to the Beginning Java forum - hopefully someone will make some suggestions.

I took a quick look on Amazon for the book you are reading - lots of positive reviews.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you learn Java?
 
Ranch Foreman
Posts: 124
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Cone wrote:How did you learn Java?


For me it started by getting my hands on two books of my dad: One about (back then new) XHTML/4.0 (at least that was the books title) and the other about JavaScript (it already had version differences between 1.0, 1.1 and 1.2 as well as dialects JavaScript, JScript and VBScript).
After playing around with those two books for quite a while I somehow moved on with the question "But how do Login-Forms work and what happen with the data send over to the server?" and got into PHP (around the time when PHP5 was going to happen).
I can't recall how long after that and why I moved on to Java ... but it was a couple of years later and I think just for the reason because the author who wrote the first two PHP books I bought myself also wrote one for Java (two actually, the original one for 1.3 and a "re-release" for 1.4.2).
I took the same approach as back with the HTML book: first read it just as a normal book - and then I started to just copy the examples and doing the excercises and modifying them ... and then extended my knowledge by porting over a game bot from php to java - which boosted me quite well in both languages.
Learning Java was quite easy as I already knew the basics of programming from PHP - but it took quite some time before I got my head around object oriented programming. In fact: After that clicked and I learned that PHP also supports oop I kind of re-learned it in the oop way.
And the past 15 years are just code, code, code ...
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@David

you can spice up your output a little by puttin '.sorted()' between .map and .forEach
 
Sheriff
Posts: 22211
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that java.io.File is pretty old. There's a new API that you can use. For example:


Note that both examples use try-with-resources. You can read this as "create a resource; execute the block; close the resource". Like regular try statements it can come with both catch and finally blocks.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob Spoor,

I tried your code (different project name) and got an error....  Did I screw something up??



Displays:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method getName(int) in the type Path is not applicable for the arguments ()
Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
The method getName(int) in the type Path is not applicable for the arguments ()

at rsdp/rsdp.rsdp.main(rsdp.java:18)


 
Marshal
Posts: 73021
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid coloured writing; some people can't read it.
If you are getting that exception, please go back to the source windows in the IDE and look for red marks to show you possible compile time errors., I think part of your error message shows what the compiler error messages ought to be. You are passing the wrong arguments to a method, for example. Make sure to correct them all. Maybe our FAQ will help.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method getName(int) in the type Path is not applicable for the arguments ()
Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
The method getName(int) in the type Path is not applicable for the arguments ()

at rsdp/rsdp.rsdp.main(rsdp.java:18)
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Cone wrote:I tried your code (different project name) and got an error....  Did I screw something up??


That is happening because now f is of type Path, not File, so you need to call f.getFileName()


Maybe name the parameter p to make it a bit more clear that it is a Path
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ron  Thanks man, got it working.

@Anyone - OK, on the Files.walk code.... it adds the base directory to the list:  "[D] Data".
                I read up a bit on streams and it looks like I could write a boolean method that would return false on recoginizing the base directory, and then use a .filter to make it go away....
               But that seems like a lot of code.  Is there an easy way to "pop" or remove the first entry in the stream? (Assuming that the first entry will always be the base directory
              or '.' as we used to say in CP/M | DOS.)

Thanks in advance,
~d <-- learning every day!
 
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The call to .filter should only be one line of code, you don't need a method to return false if the file's name is ".". You just need a simple boolean lambda expression inside the .filter call which does that.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:The call to .filter should only be one line of code, you don't need a method to return false if the file's name is ".". You just need a simple boolean lambda expression inside the .filter call which does that.



Thank you Paul for believing I would understand that!  I'm in week 2 or 3 of self-learning Java.  (I learned procedural PHP when we were in PHP 3.2, I passed a beginners VisualBasic course, and remember using line-numbers on TRS-80 Basid.)
So if I might ask...  Would my .fllter go before or after the .map (or does it matter)?  What would it look like?

Thanks  you,
~Dave the n00b.

A few digits of Pi from memory just so I feel like I know at least a little something:
3.1415 926 535 8979 323 846264 3383 2795 0 2884 1971 693 993 75 105 820 974 94459 2307 8164 0628 6208 998 6280 34825 34211 7...
That feels better  :-/
 
Paul Clapham
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to filter out the base directory and then map the rest of the files, right?

And the filter would look something like -- I didn't check the API docs so the actual code for seeing whether a file's name is "." might not quite be that.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, If anyone is interested in Memorizing Pi, I wrote a program  (in Visual Basic) that can test you on the first 512 decimal places...  It requires a full size keyboard as it only accepts the scancodes of the numeric keypad.  It's not super cool, but it's fun if you have trouble sleeping.   :-)  Am I allowed to post exteral links?   Site: DataSwag . com    Directory: /Pi/   File: Memorize.pi.exe

~d
 
Paul Clapham
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You want to filter out the base directory and then map the rest of the files, right?



Although before you start solving that problem, you might want to see what the existing code in this thread does. I've written a few directory-tree-walking things in Java and as far as I recall I never had to write any special code to ignore things which aren't files.
 
Rob Spoor
Sheriff
Posts: 22211
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Cone wrote:@Rob Spoor,

I tried your code (different project name) and got an error....  Did I screw something up??


No, I did, like Ron showed.


About filtering out the first element - you can only use "." if the argument is actually ".". All Path objects are resolved against the argument to the walk method. If that's equal to Paths.get(".") you can use ".", otherwise you can't. The best way is to compare against that argument:
Alternatively you can simply skip the first element of the stream:
The former is clearer, the latter is probably a bit faster because you don't have to compare each element.
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea!
 
David Cone
Ranch Hand
Posts: 68
Eclipse IDE MySQL Database Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@RS

It worked.... it's a path, not a string....  I had to put the .filter before the .map, but once I did that, it worked.
 
reply
    Bookmark Topic Watch Topic
  • New Topic