How to read a binary file from JAR
If you want to read a binary file from JAR file (read only), for example a game level, a map, or any data information you have to:
- Include the data file in to your project, so the IDE can append it to the JAR package.
- Use the following code to read a binary file from the JAR
private byte[] readBinaryFile(String fileName) throws IOException {
InputStream input = getClass().getResourceAsStream(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int read = input.read(); read >= 0; read = input.read())
output.write(read);
byte[] buffer = output.toByteArray();
input.close ();
output.close();
return buffer;
}


(no comments yet)