Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a PST file in Java?

Tags:

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?

like image 419
murali Karthick Avatar asked Nov 27 '14 06:11

murali Karthick


2 Answers

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.

like image 109
Mureinik Avatar answered Nov 03 '22 08:11

Mureinik


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.

like image 30
Drunix Avatar answered Nov 03 '22 09:11

Drunix