Skip to main content
Rollback to Revision 2
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

Update

Followings are the changes that I have made as per the review:

To read the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectInputStream objectInputStream = null;
    
    try {
        objectInputStream = new ObjectInputStream(Channels.newInputStream(fileChannel));
        return clazz.cast(objectInputStream.readObject());
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectInputStream);             
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}

To write the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectOutputStream objectOutputStream = null;
    
    try {
        objectOutputStream = new ObjectOutputStream(Channels.newOutputStream(fileChannel));
        objectOutputStream.writeObject(serialized);
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectOutputStream);                
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}

Update

Followings are the changes that I have made as per the review:

To read the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectInputStream objectInputStream = null;
    
    try {
        objectInputStream = new ObjectInputStream(Channels.newInputStream(fileChannel));
        return clazz.cast(objectInputStream.readObject());
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectInputStream);             
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}

To write the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectOutputStream objectOutputStream = null;
    
    try {
        objectOutputStream = new ObjectOutputStream(Channels.newOutputStream(fileChannel));
        objectOutputStream.writeObject(serialized);
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectOutputStream);                
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}
added 1741 characters in body
Source Link
Tapas Bose
  • 197
  • 1
  • 2
  • 9

Update

Followings are the changes that I have made as per the review:

To read the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectInputStream objectInputStream = null;
    
    try {
        objectInputStream = new ObjectInputStream(Channels.newInputStream(fileChannel));
        return clazz.cast(objectInputStream.readObject());
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectInputStream);             
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}

To write the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectOutputStream objectOutputStream = null;
    
    try {
        objectOutputStream = new ObjectOutputStream(Channels.newOutputStream(fileChannel));
        objectOutputStream.writeObject(serialized);
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectOutputStream);                
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}

Update

Followings are the changes that I have made as per the review:

To read the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectInputStream objectInputStream = null;
    
    try {
        objectInputStream = new ObjectInputStream(Channels.newInputStream(fileChannel));
        return clazz.cast(objectInputStream.readObject());
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectInputStream);             
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}

To write the file:

RandomAccessFile serFile = null;
FileChannel fileChannel = null;

try {
    serFile = new RandomAccessFile(file, "rw");
    fileChannel = serFile.getChannel();
    FileLock lock = fileChannel.lock();
    ObjectOutputStream objectOutputStream = null;
    
    try {
        objectOutputStream = new ObjectOutputStream(Channels.newOutputStream(fileChannel));
        objectOutputStream.writeObject(serialized);
    } catch (Exception cause) {
        cause.printStackTrace();
    } finally {
        lock.release();
        closeStream(objectOutputStream);                
    }
} catch (Exception cause) {
    cause.printStackTrace();
} finally {
    closeStream(fileChannel);
    closeStream(serFile);
}
added 9 characters in body
Source Link
Tapas Bose
  • 197
  • 1
  • 2
  • 9
  • If there is any drawback present in the code?
  • Is the right way to do it?
  • What happens if the lengthsize of the RandomAccessFile is greater thandata that I want to read or write exceeds the Integer.MAX_VALUE? How to handle that scenario?
  • If there is any drawback present in the code?
  • Is the right way to do it?
  • What happens if the length of the RandomAccessFile is greater than Integer.MAX_VALUE? How to handle that scenario?
  • If there is any drawback present in the code?
  • Is the right way to do it?
  • What happens if the size of the data that I want to read or write exceeds the Integer.MAX_VALUE? How to handle that scenario?
Source Link
Tapas Bose
  • 197
  • 1
  • 2
  • 9
Loading