Solved

how to get file count in servlets or jsp

Posted on 2009-05-09
10
265 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
The solution was provided in the question
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

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

0
 
LVL 86

Expert Comment

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

Expert Comment

by:objects
Comment Utility
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
Comment Utility
which just copied what I already posted, nice work

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now