Remove the attachment of a page in Xwiki with java class

Hello Everyone.
i have a xwiki version 16.1.0
I want to delete the attachments of a Page and replace them with some files from Filesystem. for that i wrote a java class as scheduler job.

my Code find the attachments of the page. it find “the soon to be attachment” files in filesystem too.

But it doesnt remove the attachment and set the files as attachments properly.

i use the removeAttachment(XWikiAttachment attachment) function from XwikiDocument.java class.
and for setting the new files as attachment i use setAttachment(fileName, content, xwikiContext); of XwikiDocument.java class.
for content of attachment i use this : InputStream content = new FileInputStream(file);

Do i use the right Functions?
what do i do wrong? in my java class i can find the right files and attachments but by deleting and setting them i have problem.

I would be very Thankful for your help.