Getting started in
JOAFIP usage
by the example: code
sample
more details: feature and usage
below an artless example to help unterstand basic principle:
private final
File storageDirectory
= new
File("runtime");
private
FilePersistence filePersistence;
private
IDataAccessSession session;
/**
open file persistence */
public static void
openFilePersistence() throws
Exception {
final
FilePersistenceBuilder builder = new
FilePersistenceBuilder();
builder.setPathName(storageDirectory,);
builder.setRemoveFiles(false,);
builder.setGarbageManagement(false,);
filePersistence = builder.build();
session
= filePersistence.createDataAccessSession();
}
/** close file persistence
saving */
public static void
closeFilePersistenceSave() throws
Exception {
if (session.isOpened()) {
session.close(EnumFilePersistenceCloseAction.SAVE);
}
filePersistence.close();
}
/** close file persistence
do not save */
public static void
closeFilePersistenceDoNotSave() throws
Exception {
if (session.isOpened()) {
session.close(EnumFilePersistenceCloseAction.DO_NOT_SAVE);
}
filePersistence.close();
}
/**
open session */
public
static void openSession() throws Exception {
session.open();
}
/**
close session saving */
public
static void closeSession() throws Exception {
session.close(EnumFilePersistenceCloseAction.SAVE);
}
/**
write an object */
public
static void writeObject(final String
key, final
Object object) throws
Exception {
session.setObject(key,
object);
}
/**
read an object */
public static Object readObject(final String
key) throws Exception {
return session.getObject(key);
}
public static void
main(final
String[] args) {
try {
openFilePersistence();
openSession();
MyEntity entity=getObject("myEntity");
if(
entity==null ) {
entity=new MyEntity();
setObject("myEntity",entity);
}
entity.anOperation(...);
closeSession();
... some other operations ...
closeFilePersistenceSave();
}
catch(Exception
e) {
e.printStackTrace();
}
}
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
© 2007-2012, joafip