Thanks for you advice.I will avoid direct commands as possible.

But since truncate provide good option for a reference file(-r), it will be easy for my operation.
And I do not want to take care of exceptions for this high-leve function.