1

Folder/File creation in java are dependent on the user from which you're executing the code. My app is creating the user-specific folder/files using Runtime.exec but the thing is now we are going to support multiple OS, accordingly the commands I'm executing are going to change.
Is there any pure java specific solution where folder/files will be created by specific user's ownership?

2
  • 2
    How about java.nio.file.Files.CreateDirectory and java.nio.file.Files.CreateFile? Commented Apr 11, 2018 at 12:16
  • You mean to say PosixFilePermission will be useful to set the permissions to Folder/Files but that will not allow us to change the ownership isn't it? Commented Apr 11, 2018 at 13:48

1 Answer 1

2

It's fairly easy, you just need to create the file as normal, then call Files.setOwner(). Alternatively, create a file with Files.createFile(), and then make sure you pass in a file attribute with the expected owner.

Note that in most operating systems, your process will need to run as privileged process to assign files to a different user.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.