Java resource files

I am writing a small graphical application that contains some "editor" functionality, and something that I would like to allow users to open multiple test text files in order to quickly test them. The easiest way to do this is to package a separate zip with the corresponding sample files and open them manually; I would like to make things more user friendly and let them select files from the application and then run them.

So ... what am I using? I originally considered .properties, but that doesn't seem to work well ...

+1


source to share


2 answers


You can include the resource file directly in the jar and then open it as a resource stream in your application. If you are using Spring, you can inject the resource directly into the bean. If not, check Class.getResourceAsStream () . You just need to be careful about the path you use to access the resource file.



+2


source


Your file dialog can be assigned the FilenameFilter file, which filters files by any criteria. You can by default point it to the sample files directory, ignore anything not named ".sample" or "MySampleXXXX.java" for example.



myDialog.setFilenameFilter( new FilenameFilter() {

   public void accept (File dir, String name) {
           return name.startsWith("FooBar");
   }
}

      

0


source







All Articles