I have a static utility class, FileUtils
which has the following method:
public static Object readDataFromInternalStorage(String fileName)
{...}
public static synchronized void
writeDataToInternalStorage(Serializable theData, String fileName)
{...}
I am encountering the odd java.io.EOFException in my read method when trying to read from internal storage. This is a multi-threaded app where multiple threads can access this code at the same time (though this is not a common case). After thinking about this for a bit, my suspicion is that while reading the file, another thread comes along and writes to the file before the read is finished, thereby mangling the read. Is my assumption correct?
Assuming it is, what is the best way to fix this? Is the answer simply to add 'synchonized' to my read method, which if I read this post correctly locks on the entire class?