I have a method where I open a geotiff file and print its dimensions
public void parseAndSaveFile(String pathFileToParse) throws IOException, Exception {
GridCoverage2DReader reader = null;
GridCoverage2D image = null;
BufferedImage img = null;
try {
File f = new File(pathFileToParse);
ParameterValue<OverviewPolicy> policy = AbstractGridFormat.OVERVIEW_POLICY.createValue();
policy.setValue(OverviewPolicy.IGNORE);
// this will basically read 4 tiles worth of data at once from the disk...
ParameterValue<String> gridsize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();
//gridsize.setValue(512 * 4 + "," + 512);
// Setting read type: use JAI ImageRead (true) or ImageReaders read methods (false)
ParameterValue<Boolean> useJaiRead = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
useJaiRead.setValue(true);
reader = new GeoTiffReader(f);
//reader.read(new GeneralParameterValue[] { policy, gridsize, useJaiRead });
image = reader.read(new GeneralParameterValue[]{policy, gridsize, useJaiRead});
//Rectangle2D bounds2D = image.getEnvelope2D().getBounds2D();
// calculate zoom level for the image
GridGeometry2D geometry = image.getGridGeometry();
img = ImageIO.read(f);
//WritableRaster raster = img.getRaster();
//int numBands = raster.getNumBands();
int width = img.getWidth();
int height = img.getHeight();
logger.info("Width: {}, Height: {}", width, height);
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.dispose();
image.dispose(true);
}
}
In the calling method, I try to delete the file
Files.deleteIfExists(Paths.get(pathFileToParse));
but I get the error:
Unable to access the file. The file is used by another process.
Because? Did I correctly release the geotiff file?
asked Apr 16, 2018 at 14:42
-
Try to see which other process is using the file - stackoverflow.com/questions/3565218/…Ian Turton– Ian Turton2018年04月16日 14:52:40 +00:00Commented Apr 16, 2018 at 14:52
-
I saw the post and installed the "Handle" utility. The only process which use the file is javaw.exeAlessio Frabotta– Alessio Frabotta2018年04月16日 15:08:16 +00:00Commented Apr 16, 2018 at 15:08
1 Answer 1
That code works perfectly for me on Linux, so unless you are doing something odd in the rest of the program it is a windows issue.
I added this main
method:
public static void main(String[] args) throws IOException, Exception {
ParseAndDelete me = new ParseAndDelete();
String pathFileToParse = "test3.tif";
me.parseAndSaveFile(pathFileToParse);
Files.deleteIfExists(Paths.get(pathFileToParse));
}
Does that work for you?
answered Apr 16, 2018 at 15:24
-
I don't know to explain why it doesn't work for meAlessio Frabotta– Alessio Frabotta2018年04月16日 15:57:10 +00:00Commented Apr 16, 2018 at 15:57
-
even the changes you proposed to me do not workAlessio Frabotta– Alessio Frabotta2018年04月19日 09:53:38 +00:00Commented Apr 19, 2018 at 9:53
-
so a program of just that main and your method fails on windows? It may be a bug but afaik none of the GeoTools devs uses windows so it is unlikely to get fixed.Ian Turton– Ian Turton2018年04月19日 10:07:55 +00:00Commented Apr 19, 2018 at 10:07
-
the process that blocks the file is javaw.exeAlessio Frabotta– Alessio Frabotta2018年04月19日 13:19:28 +00:00Commented Apr 19, 2018 at 13:19
-
I solved with these instructions: FileInputStream tiffFile = new FileInputStream(pathFileToParse); ImageInputStream iis = ImageIO.createImageInputStream(tiffFile); BufferedImage img = ImageIO.read(iis); Just close the FileInputStream and I no longer have problems deleting the file. If I try to close the ImageInputStream instead I have an error because it is already closedAlessio Frabotta– Alessio Frabotta2018年04月19日 14:09:08 +00:00Commented Apr 19, 2018 at 14:09
lang-java