Text file in project workspace written into by a Jenkins job does not give latest data when read again

I have a maven jenkins job which executes a TestNG test. This test makes a GET call to an API which returns a number in the response. This response is then stored in a .txt file which is located in a project folder (for eg /TestData/). The API returns a new number in intervals and my job is supposed to read this number, compare it with number existing in the .txt file and update it if it’s not same.

Flow of my program is:
Assumption: number.txt file already exists and contains value ‘0’
1. Program makes a GET call to /getNumber API
2. /getNumber API returns ‘1’
3. Open file number.txt and read value from it.
4. Compare value from file and value from API.
5. If values are different
update(write) file number.txt with value ‘1’
Print “Value is updated”
Print “Value is not updated”
6. End.

What’s my issue
This works perfectly fine when I run the TestNG test locally on my system. But if I run it as a Jenkins job, it fails to read the updated value from the .txt file (value written in based on previous API call that returned ‘1’), instead it considers 0 as the value everytime and thus keeps printing “Value is updated”.

What could be the reason this is happening? Is there any other way I can handle this function without writing to a file on Jenkins?