How to access files stored in a module?

I’m programming some custom code for a module I’m working on, and after some trial-and-error have got things more or less figured out. Now I’m up against what is (probably/hopefully) a simple problem, but the solution isn’t obvious to me. I want to store some data files within my module, to read using code (for use in defining some custom piece behavior, for example). What path do I use to access such a file?

Given a test file “foo” placed in the root of the module (same location as buildFile and moduledata), I have tried to create a new FileInputStream on “foo” and “/foo”, getting a FileNotFound exception in both cases. I figure there must be a way to do this, since VASSAL must do it when loading images stored in the module, but the VASSAL code is sufficiently arcane and overloaded that I can’t readily figure out what to do. Any assistance would be appreciated.


Leland J. Tankersley

Ah, some additional digging allowed me to answer my own question. In case someone from the future comes here looking for the answer to the question:

Look at VASSAL.tools.DataArchive; it is a wrapper for accessing members within a zip archive (in this case, a module file). There are convenience routines for accessing images in the images/ directory (which is how I eventually found it), but you can also do things like:

String myFile = "foo"; try { InputStream is = GameModule.getGameModule().getDataArchive().getInputStream(myFile); DataInputStream in = new DataInputStream(new BufferedInputStream(is)); // do stuff with in } catch (FileNotFoundException e) { } catch (IOException e) { }

which will open the file named “foo” located in the root of the module archive.

Note that you really, really should have a finally block which properly closes the stream here:

final	String myFile = "foo";
InputStream is = null;
try {
  is = GameModule.getGameModule().getDataArchive().getInputStream(myFile);

  DataInputStream in = null;
  try {
    in = new DataInputStream(new BufferedInputStream(is));
    // do stuff with in
    in.close();
  }
  finally {
    IOUtils.closeQuietly(in);
  }
}
catch (FileNotFoundException e) {
  // handle the exception
}
catch (IOException e) {
  // handle the exception
}
finally {
  IOUtils.closeQuietly(is);
}

Good point.