Generate files with all permissions

Asked

Viewed 189 times

1

I am generating *.csv files from this:

BufferedWriter strW = new BufferedWriter(new FileWriter(caminhoCSV.toString()))

However the generated files are only with read permission, how could you change this so that the generated file has read and change permission?

2 answers

2


You can really use the class File after the file is generated

String caminho = "caminho/do/meu/arquivo";

final File file = new File(caminho);
file.setReadable(true, false);
file.setExecutable(true, false);
file.setWritable(true, false);
  • final File is necessary or only File is enough?

  • does not necessarily need to be final but use to ensure that you are not modifying the value improperly

  • 1

    Perfect, worked as needed +1

1

You can call the Unix command, if you are using an O.S based on it, through Runtime.exec, note:

Runtime.getRuntime().exec("chmod 666 caminho_arquivo");

Be careful when concatenating a string, it has to have a space between the 666 and the file path.

  • could explain how this command works?

  • Certainty that this works independent of the operating system?

  • R. Santos, chmod is a Unix command to change the permissions of a file, there are several 'numbers' that represent the permissions of the file, involving group permissions, public permissions, read, record and execute, etc. When you run this line of code I gave you, you are running a shell command, and this command, as already explained, is changing the permission of a file (666, in this case, is read and write at all levels). I think all Unix-based operating systems have that system.

  • If running in windows will not work, and as the question is not clear that it is linux, it is good you edit the answer containing this information.

  • I figured it out, edited the answer already, thank you!

  • Thanks for the tip but as it doesn’t necessarily need to be linux the environment where the application will run there will be inconsistencies +1

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.