java get file paths

Posted by user283188 on Stack Overflow See other posts from Stack Overflow or by user283188
Published on 2010-04-23T22:10:00Z Indexed on 2010/04/23 22:13 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

hey everybody,

I have a jsp page which contains the code which prints all files in a given directory and their file paths. The code is

if (dir.isDirectory())
        {
            File[] dirs = dir.listFiles();
            for (File f : dirs)
            {
                if (f.isDirectory() && !f.isHidden())
                {
                    File files[] = f.listFiles();
                    for (File d : files)
                    {
                        if (d.isFile() && !d.isHidden())
                        {
                            System.out.println(d.getName()+ 
                            d.getParent() + (d.length()/1024));
                        }
                    }
                }
                if (f.isFile() && !f.isHidden())
                {
                    System.out.println(f.getName()+ 
                    f.getParent() + (f.length()/1024));
                }
            }
        }

The problem is that it prints the complete file path, which when accessed from tomcat is invalid. For example, the code spits out the following path:

/usr/local/tomcat/sites/web_tech/images/scores/blah.jpg

and I want it to only print the path up to /images ie

/images/scores/blah.jpg

I know I could just mess around with an actual string, ie splitting it or string matching, but is there an easier way to do it?

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about jsp