views:

898

answers:

2

I'm writing a small GUI app that contains some "editor" functionality, and something that I'd like to let users open a few sample text files to test things out quickly. The easiest way of doing this would be to package a separate zip with the appropriate sample files, and have them open them manually; I'd like to make things a little more user-friendly and allow them to pick the files from inside the application and then run them.

So... what do I use? I initially considered .properties but that doesn't seem terribly well suited for the job...

A: 

Your FileDialog can be given a FilenameFilter that filters files by any criteria you like. You can default-point it to a directory of sample files, have it ignore everything not named ".sample" or "MySampleXXXX.java", e.g.

myDialog.setFilenameFilter( new FilenameFilter() {

   public void accept (File dir, String name) {
           return name.startsWith("FooBar");
   }
}
Steve B.
+1  A: 

You can include a resource file right in the jar and then open it as a resource stream in your app. If you're using Spring, you can inject the resource right into a bean. If not, check out Class.getResourceAsStream(). You just have to be careful about the path you use to get to the resource file.

Don Kirkby