How to close a PST file in Java?
Asked Answered
A

3

20

I am using java-libpst.0.7.jar for reading PST messages. I am using the following code to open the PST file to read messages.

PSTFile pstFile = new PSTFile("Path of the pst file");

I have to close the PST file once after getting the message details. But there is no option to close the PST file. How can I do this?

Afro answered 27/11, 2014 at 6:53 Comment(1)
I have an application to read the PST files. The application could not read the pst file which is access in some other application. If I use the above code, the application could not read the pst file.The application can read once after restart the system.Afro
U
50

By reading the code, it's apparent that libpst indeed does not expose a "close" method. The finalize() method does close the underlying file when the PSTFile is garbage-collected, so I'd recommend to use it in the smallest scope possible and dispose of it ASAP, but other than that there's not much you can do (except reporting an issue to the project - or better yet, sending a patch yourself, of course).

EDIT 1:
PSTFile has a getFileHandle() method which returns the underlying file, so you could close() that:

PSTFile pstFile = new PSTFile("Path of the pst file");
// use the file
pstFile.getFileHandle().close();

EDIT 2:
I've created a pull request to add PSTFile.close(). Let's see how it fans out.

EDIT 3:
My pull request has been merged (thanks Richard Johnson!). In the next release (or if you build java-libpst by yourself) you'll be able to call close() on a PSTFile directly.

Upswell answered 27/11, 2014 at 7:3 Comment(0)
H
1

PSTFile closes the filehandle in its finalize method, so the file is closed when your PSTFile is garbage collected. I don't think this is good style to clean up resources this way because finalize may be called much later or even never.

Haldan answered 27/11, 2014 at 7:3 Comment(2)
I have an application to read the PST files. The application could not read the pst file which is access in some other application. If I use the above code, the application could not read the pst file.The application can read once after restart the system.Afro
Probably because the file is still not closed. Ending your application closes the filehandle. As Mureinik and Adrian B. have pointed out, I would not use the library in the current state. Maybe PSTFile could implement AutoCloseable.Haldan
A
1

Like @RC said, the library does not expose a close method. Look at https://github.com/rjohnsondev/java-libpst/blob/master/src/main/java/com/pff/PSTFile.java . The author overrode the finalize() method, expecting the Stream to magically close at garbage collection.

/**
  * destructor just closes the file handle...
  */
@Override
protected void finalize() throws IOException {
    in.close();
}

However, garbage collection is not guaranteed throughout the life of the application, and even so, neither is the run of finalize(). See one of the many articles on the net explaining why: http://howtodoinjava.com/2012/10/31/why-not-to-use-finalize-method-in-java/ .

My advice would be to go for a more "proven" library out there or, if not possible, change that file to expose an explicit close method and rebuild it.

Abduce answered 27/11, 2014 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.