Reputation: 18586
I was wondering wether it is possible to create or simulate a file with a content set at creation and the assurance that nobody can ever change the file. If possible, can I do it in java?
Upvotes: 5
Views: 20410
Reputation: 9
In this case simply you have to create a file using File class after that by reference you have to call setReadOnly() method.
File f=new File("abc.txt");
f.setReadOnly();
Upvotes: 0
Reputation: 1
Read-only way can be useful altough it is not impossible to change or crack.So if you can not create an unreachable file then you can convert its contents to not understandable thanks to some kind of Encryption algorithms.For example AES may be so usefull.
Upvotes: 0
Reputation: 6822
Try:
Upvotes: 2
Reputation: 396
Setting a file to read only is not going to make it so no one can ever change it. It takes about 3 seconds to unset the read only flag. The file can then be opened in a hex editor or other program that can handle the file type and changes can be made.
Upvotes: 7
Reputation: 3869
To make it so setWritable(true) does not enable writing again you could extend File and override the setWritable() methods.
import java.io.File;
import java.io.IOException;
import java.lang.Override;
public class FileAttributeDemo2 {
private static class ReadOnlyFile extends File {
public ReadOnlyFile(String pathname) {
super(pathname);
}
@Override
public boolean setWritable(boolean writeable) {
return setWritable(writeable, true);
}
@Override
public boolean setWritable(boolean writeable, boolean ownerOnly) {
if (!writeable) {
return super.setWritable(false, ownerOnly);
}
return false;
}
}
public static void main (String[] args) throws IOException {
File file = new ReadOnlyFile("test.txt");
if (file.exists()) {
file.delete();
}
file.createNewFile();
System.out.println("Before. canWrite? " + file.canWrite());
file.setWritable(false);
System.out.println("Set writable false. canWrite? " + file.canWrite());
file.setWritable(true);
System.out.println("Set writable true. canWrite? " + file.canWrite());
}
}
which produces the output:
Before. canWrite? true
Set Writable False. canWrite? false
Set Writable True. canWrite? false
Upvotes: 1
Reputation: 40995
If you simply just need to create a Read-Only file, then won't the code below be sufficient? Unless I am missing something from your question:
import java.io.File;
import java.io.IOException;
public class FileAttributesDemo {
public static void main(String[] args) throws IOException {
// Create a new file, by default canWrite=true, readonly=false
File file = new File("test.txt");
if (file.exists()) {
file.delete();
}
file.createNewFile();
System.out.println("Before. canWrite?" + file.canWrite());
// set to read-only, atau canWrite = false */
file.setWritable(false);
System.out.println("After. canWrite?" + file.canWrite());
}
}
Upvotes: 2
Reputation: 2489
yes we can make read only file in java using setReadOnly() method.
After using this method, you will not be able to write or edit into the file.
import java.io.File;
public class FileReadOnly {
public static void main(String[] args) {
File file = new File("c:/file.txt");
file.setReadOnly();
System.out.println("File is in read only mode");
}
}
or in this way also.
import java.io.File;
import java.io.IOException;
public class FileAttributesDemo {
public static void main(String[] args) throws IOException {
// Create a new file, by default canWrite=true, readonly=false
File file = new File("test.txt");
if (file.exists()) {
file.delete();
}
file.createNewFile();
System.out.println("Before. canWrite?" + file.canWrite());
// set to read-only, atau canWrite = false */
file.setWritable(false);
System.out.println("After. canWrite?" + file.canWrite());
}
}
Upvotes: 3