Geertjan's Blog

  • January 28, 2011

Incorporating a File into a NetBeans Platform Application

Geertjan Wielenga
Product Manager
In my layer:
<folder name="Profiles">
<file name="DriverProfiles" url="nbresloc:/com/visitrend/ndvis/sql/gui/resources/DriverProfiles.xml"/>

The url above points to a package in a module, which contains the actual file I need to incorporate, i.e., "DriverProfiles.xml".

Now, in my code:

try {
URL driverProfiles = new URL("nbfs:/SystemFileSystem/Profiles/DriverProfiles");
FileObject fo = URLMapper.findFileObject(driverProfiles);
Document doc = doc = XMLUtil.parse(new InputSource(fo.getInputStream()), true, true, null, null);
Element e = doc.getDocumentElement();

Join the discussion

Comments ( 1 )
  • Jesse Glick Monday, January 31, 2011

    You probably want nbres, not nbresloc here, unless you seriously intend for this file to be localized.

    Avoid relying on the nbfs URL syntax. Anyway this is too complicated. Just use: FileUtil.getConfigFile("Profiles/DriverProfiles")

    Do not use new InputSource(fo.getInputStream()) as this does not set the URL correctly, so e.g. relative entity references will not work. Anyway that code leaks an input stream (never explicitly closes it). Rather use new InputSource(fo.getURL().toString()). Also consider passing a real error handler, such as XMLUtil provides; otherwise parser warnings get dumped to stderr.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.