Jeg har følgende klasse for at simplificere læsning af filer:
package se.util;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
* Simplifies loading files. Works from inside jars
*
* @author Søren Enevoldsen
*
*/
public class ResourceHandler {
/**
* Gets an input stream from the path
*
* @param path The path
* @return The input stream
* @throws FileNotFoundException If file could not be found
*/
public static InputStream getStream(String path) throws FileNotFoundException {
InputStream stream = ClassLoader.getSystemResourceAsStream(path);
if (stream != null)
return stream;
else
throw new FileNotFoundException();
}
/**
* Gets an BufferedImage from the path
*
* @param path The path
* @return A BufferedImage
* @throws FileNotFoundException If file could not be found
* @throws IOException If error during read
*/
public static BufferedImage getImage(String path) throws FileNotFoundException, IOException {
return ImageIO.read(getStream(path));
}
}
Vil filhandlesne automatisk blive lukket ( close() ) når jeg ikke bruger dem længere? F.eks. hvis jeg kalder getImage, vil den stream så blive lukket?
Hvis ikke har nogen så et forslag til hvordan det ellers skal gøres?
Mange tak.