3

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
2
  • Try to see which other process is using the file - stackoverflow.com/questions/3565218/… Commented Apr 16, 2018 at 14:52
  • I saw the post and installed the "Handle" utility. The only process which use the file is javaw.exe Commented Apr 16, 2018 at 15:08

1 Answer 1

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
5
  • I don't know to explain why it doesn't work for me Commented Apr 16, 2018 at 15:57
  • even the changes you proposed to me do not work Commented 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. Commented Apr 19, 2018 at 10:07
  • the process that blocks the file is javaw.exe Commented 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 closed Commented Apr 19, 2018 at 14:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.