Solved

how to get file count in servlets or jsp

Posted on 2009-05-09
10
268 Views
Last Modified: 2013-11-24

hi i want to know how to get no.of files in a directory.

of course i use this code
 int filecount=new File("images/ln/headings").listFiles().length;  
 to find the no.of files in the images/ln/headings directory. but i get always returns value 8 even the ./headings folder contains 10 or 20 files.

my doubt is this .class file is in app/WEB-INF/classes folder, but my targeted file is in app/images/ln/headings folder. so how can i use the above code to getting the file count in images/ln/headings folder..
0
Comment
Question by:haneefnb
  • 5
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24342743
its not where the class file is, it is where it is run from
use the absolute path to make sure you get the right directory
If its inside a webapp you can use the following to get the actual path of a file in the webapp

http://helpdesk.objects.com.au/java/how-to-get-the-path-of-a-file-in-a-web-application

0
 

Author Comment

by:haneefnb
ID: 24343067
thanks for u r reply

but what about the jsp code so please provide the jsp code for solving above problem

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24343499
In JSP you could do the following but don't forget 'filecount' will count directories too


 int filecount=new File(application.getRealPath("images/ln/headings")).listFiles().length;  

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 92

Expert Comment

by:objects
ID: 24346027
> but what about the jsp code so please provide the jsp code for solving above problem

would be exactly the same, no need to change anything.


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24421521
The solution was provided in the question
0
 
LVL 92

Expert Comment

by:objects
ID: 24421556
I provide the solution here, what problem did you have with it?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24421579
You wanted to know how to do this in jsp - i showed you at http:#24343499
0
 
LVL 92

Expert Comment

by:objects
ID: 24421653
was answered in my first post

its not where the class file is, it is where it is run from
use the absolute path to make sure you get the right directory
If its inside a webapp you can use the following to get the actual path of a file in the webapp

http://helpdesk.objects.com.au/java/how-to-get-the-path-of-a-file-in-a-web-application


What problem did you have with it?

0
 
LVL 92

Expert Comment

by:objects
ID: 24421742
which just copied what I already posted, nice work

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24421957
:-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 39
eclipse with Angularjs2.0 3 35
Error in @AspectJ Based AOP with Spring 2 13
how to install java on RHEL image on EC2 4 26
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question