required standard open options for creating or replacing a file


I just found a bug on my own code.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE, // create if not exist
    StandardOpenOption.WRITE // i'm gonna write on
);

Above code seemed working, but when I tried to replace a bigger file with a smaller one, the file size remains and the only leading(?) portion of file contents replaced.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE,
    StandardOpenOption.TRUNCATE_EXISTING, // truncate the file!!!
    StandardOpenOption.WRITE
);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s