Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Won't count files in a directory

I'm trying to count the number of files in a directory, to determine navigation.  Here's what I have:

<%@page contentType="text/html" import="java.io.*"%>

//Get the parameters
String courseId          = request.getParameter("courseid");
int chapter          = Integer.parseInt(request.getParameter("chapter"));
int pageIndex          = Integer.parseInt(request.getParameter("page"));
String state          = request.getParameter("state");

//Get the number of pages for the current chapter

String directory = courseId + "\\" + chapter;
int currentChapPages      = 0;
File chapPages = new File(directory);
if (chapPages.isDirectory())  {
     currentChapPages = chapPages.listFiles().length;

//Calculate the next and previous chapter & page
int prevChapter          = chapter;
int prevPage          = pageIndex - 1;
int nextChapter          = chapter;
int nextPage          = pageIndex + 1;
boolean showNextButton     = true;
boolean showPrevButton     = true;

//If the current page is the last page, go to the next chapter and first page
if (pageIndex >= currentChapPages) {
     nextPage = 1;

     //Make sure there is a next chapter, if not don't show next button
     String directoryPrev = courseId + "\\" + chapter;
     int prevChapPages      = 0;
     File prevPages = new File(directoryPrev);
     if (prevPages.isDirectory())  {
          prevChapPages = prevPages.list().length;
     else {
          showNextButton = false;

//If it is the first page, go to the previous chapter except for the How-To (98)
if (pageIndex <= 1 && chapter != 98) {

     //If the the scope is beyond the first chapter, go to the foreword (99)
     if (prevChapter <= 0) {
          prevChapter = 99;

     //Find the last page of the previous chapter if it is not the current chapter
     //Set the prev page to the last page in the previous chapter
     if (prevChapter != chapter) {
          String directoryPrev = courseId + "\\" + chapter;
                int prevChapPages      = 0;
                File prevChapList = new File(directoryPrev);
                if (prevChapList.isDirectory())  {
                    prevChapPages = prevChapList.list().length;
          prevPage = prevChapPages;

Seems like it should work, but if I print out the value of currentChapPages or prevChapPages I get zero.  If I declare the variable without assigning it an initial value, it throws a 500 saying "The variable "currentChapPages" may be accessed here before having been definitely assigned a value."  We're running JRun 3.0 on IIS 5.  Anyone have any ideas why I can't get a value out of the currentChapPages bit?

  • 3
  • 2
1 Solution
>> String directory = courseId + "\\" + chapter;
>> File chapPages = new File(directory);
your directory is not pointing to the right place

where is your directory located, if it is localted at yourwebapproot/courseId/chapter, try:

String directory = application.getRealPath( "/" + courseId + "/" + chapter );
create a File object refer a directory
File fdir = new File("path/url to dir");

the following returns name of files or directory
String files[] = fdir.list();
tigerjade_ncAuthor Commented:
The path's correct.  I tried making it full relative as well as absolute, and still didn't get any closer.  The currentChapPages & prevChapPages variables still return zero, no matter what's in the directory.  Neither of these suggestions worked with this bleeping thing.  :(  Anything else I might have missed?  Thanks for the efforts, y'all.  
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

tigerjade_ncAuthor Commented:
I found a weird work-around.  I moved the files to another level, and now it's counting!  Thanks, y'all!
>> I found a weird work-around.  I moved the files to another level
I swear that your path is pointing to the right place, and I would never use filesystem relative path, because it is different from server to server. if you must use relative path, use your webapp's relative path with application.getRealPath method as I suggested.

Thanks for your points and good luck :)
>> I swear that your path is pointing to the right place
i mean "NOT pointing", sorry :)

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now