Solved

how to get file count in servlets or jsp

Posted on 2009-05-09
10
271 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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