Results 1 to 2 of 2

Thread: Help loading a list of files from directory

  1. #1
    Join Date
    May 2012
    Posts
    9

    Default 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. #2
    Join Date
    May 2012
    Posts
    9

    Default

    solved,

    I did this:

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •