Reputation: 9
I have a Java app that uses a .txt file as a database. Every time I run the program it reads the file and copies its contents into an array. After I'm done the data in the array will have changed, and since I don't know any way of deleting lines in a file I update the file by deleting it, creating a new one with the same name and writing back the content of the array to the file.
This works if I always run the program from the same user (my OS is Ubuntu by the way). But if I run the program from user A, it will create a file that only has read/write permission for user A. When I attempt to run the program from user B, it will be able to read the file, but it won't be able to delete it in order to update it because it does not have permission to delete the file.
My question is: is there any way to make my Java program create a file that has read/write permission for everyone? or is there any better way to update the contents of the file without having to delete it? maybe there is a better alternative to using files altogether to store data once the program is closed?
P.D. This is my first time asking in StackOverflow, so please bear with me if I didn't write the question properly or I missed some details you might need. Also I am still pretty noob with programming, so I'm sorry in advance if I asked any stupid questions. Thank you very much for your time and for your answers.
Upvotes: 1
Views: 129
Reputation: 174
If this is a Posix-conforming system, set the Posix file permissions to whatever you want.
See Files.setPosixFilePermissions.
By the way, you don't have to explicitly delete the file, you can just overwrite it.
Upvotes: 1