Announcement Announcement Module
Collapse
No announcement yet.
Help loading a list of files from directory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help loading a list of files from directory

    Hi all,

    Ok so I defined a bean to inject the path of the DIR of the files I want to load. Doing a getFiles() I get the file but now the format of the paths changed.

    Code:
    public void setJadDir(Resource jadDir) {
    		Assert.isTrue(jadDir.exists());
    		try {
    			File directory = jadDir.getFile();
                            File[] files = directory.listFiles();
    			for (File file : files) {
    				if (!file.isFile()) {
    At this stage jadDir's path if I keep my mouse over the variable is "/WEB-INF/jadjars". Great, but keeping my mouse over file shows "C:/java/apps/...../WEB-INF/jadjars/myjad.jad"

    Why did the format now change?

    The problem with this is that,

    Code:
       inputStream = context.getResourceAsStream(file.getPath())
    does not work.

    So fine I then tried to substring that long filepath to the short version which does work - eg. I want /WEB-INF/jadjars/myjad.jad cause that works fine.

    Code:
    String contextPath = context.getContextPath();       
    int idx = contextPath.indexOf(contextPath + File.separatorChar + "WEB-INF");
    Guess what, idx == -1, because even though im running a Windows box, the path that context spits out countains forward slashes, while File.separatorChar is a backslash.

    So I'm screwed.

    What now?

  • #2
    solved,

    I did this:

    Code:
    Resource jadDir;
    String path = jadDir.getFilename();
    String filename = file.getName();
    context.getResourceAsStream("/WEB-INF/" + path + "/" + filename);

    Comment

    Working...
    X