public class PersistentObjectStorage
extends java.lang.Object
Constructor and Description |
---|
PersistentObjectStorage(java.lang.String directoryName)
Before you use any other method of this library you have to set a storage path.
|
Modifier and Type | Method and Description |
---|---|
void |
firstStartFinished()
If called, an empty file
notFirstStart is created in the storage path. |
java.lang.String |
getStoragePath()
Get the current set storage path as a
String . |
boolean |
isFirstStart()
Checks if the file
notFirstStart exists, so you can check if the application was started once before. |
static void |
main(java.lang.String[] args) |
<T extends java.io.Serializable> |
read(java.lang.String fileName)
Reads an object of a given type from a
File . |
void |
resetAllData()
Deletes the directory where all files created with this library are stored.
|
boolean |
resetFirstStart()
Deletes the file
notFirstStart in the storage path if it exists. |
<T extends java.io.Serializable> |
write(T object,
java.lang.String fileName)
Writes an object of a given type to a
File . |
public PersistentObjectStorage(java.lang.String directoryName)
directoryName
which will be created in the home path of the current user.
directoryName
gets trimmed.directoryName
- The name of the directory inside the home directory of the current user where all files are created and read with this library.java.lang.NullPointerException
- If directoryName
is nulljava.lang.IllegalArgumentException
- If directoryName
is emptypublic java.lang.String getStoragePath()
String
.public boolean isFirstStart()
notFirstStart
exists, so you can check if the application was started once before.notFirstStart
doesn't exist in the storage pathpublic void firstStartFinished() throws java.io.IOException
notFirstStart
is created in the storage path.
From now on isFirstStart
returns false.java.io.IOException
- If an I/O error occurredpublic boolean resetFirstStart()
notFirstStart
in the storage path if it exists.
From now on isFirstStart
returns true.public <T extends java.io.Serializable> T read(java.lang.String fileName) throws java.io.IOException, java.lang.ClassNotFoundException
File
.T
- The type of the object that is readfileName
- The name of the file an object should be read fromjava.lang.NullPointerException
- If fileName
is nulljava.io.FileNotFoundException
- If the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.java.io.IOException
- If an I/O error occurredjava.lang.ClassNotFoundException
- Class of a serialized object cannot be foundjava.io.InvalidClassException
- Something is wrong with a class used by serializationjava.io.StreamCorruptedException
- Control information in the stream is inconsistentjava.io.OptionalDataException
- Primitive data was found in the stream instead of objectsjava.lang.ClassCastException
- If wrong class for T is givenpublic <T extends java.io.Serializable> void write(T object, java.lang.String fileName) throws java.io.IOException
File
.T
- The type of the object that is writtenobject
- The object of a given type that should be writtenfileName
- The name of the File
the object should be written tojava.lang.NullPointerException
- If fileName
is nulljava.io.FileNotFoundException
- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reasonjava.io.InvalidClassException
- Something is wrong with a class used by serializationjava.io.IOException
- If an I/O error occurredpublic void resetAllData()
isFirstStart
returns true.public static void main(java.lang.String[] args)