Read in a file from the WAR

Created: 3 December 2008  Modified:

Originally published on chrislynch.info website.

Below is how to access and read in a file located in a war file. It might be possible to write out to it as well but I haven’t tried that.

    int size = 1000, bytesRead = 0;
    byte dataBytes[] = new byte[size];
    try {
      DataInputStream dis = new DataInputStream(FormManipulator.class.getResourceAsStream("file.xml"));
      while((bytesRead = dis.read(dataBytes,0,size)) > 0) {
        content += new String(dataBytes,0,bytesRead);
      }
      dis.close();
    } catch(IOException ioe) {
      System.out.println("***************************************");
      System.out.println(ioe.toString());
      System.out.println("***************************************");
    }
tags: blog - old blog - Java
   Memory is Fleeting