Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Java File handling – when is a file actually saved

37 sec read

Consider the following code….

File ff=File.createTempFile(“page”,”.bin”, new File(ObjectStore.temp_dir));

BufferedOutputStream to = new BufferedOutputStream(new FileOutputStream(ff));

to.write(currentDisplay.serializeToByteArray(null));         

to.flush();           

to.close();

pagesOnDisk.put(key,ff.getAbsolutePath());

It stores a serialised Java Object (currentDisplay) on disk and then stores the file location so we can reuse it. So in theory,  if the value is in the Map pagesOnDisk, we should be able to retrieve the data and reuse it…

Unfortunately, that is not always the case. While Java may think the file has been written out, an attempt to immediately reuse it results in alsorts or errors arising from trying to read a File which has not fully been written out to disk.  At the OS system level, the file has been Buffered and is still being written out.

So be aware of this ‘gotcha’ in Java and either ensure that there is a sufficient time delay to allow the data to be written out, or include some check to make sure the data is valid.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *