Create read-only file

I was wondering if it is possible to create or simulate a file with the content set during creation, and the belief that no one can modify the file. If possible, can I do this in java?

+4
source share
6 answers

Setting up a read-only file is not going to make it impossible for anyone to change it. It takes about 3 seconds to clear the read-only flag. The file can then be opened in a hex editor or other program that can process the file type and changes can be made.

+6
source

yes, we can only make the read file in java using the setReadOnly () method.

After using this method, you will not be able to write or edit 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 that way too.

 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()); } } 
+2
source

If you just just need to create a read-only file, will this code not be below? If I haven't missed any of 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()); } } 
+2
source

Try:

  • file.setReadOnly ()
  • Runtime.getRuntime (). exec ("attrib" + "+ file.getAbsolutePath () +" "+" + R "); // (works on Windows)
+2
source

To make setWritable (true) not include the record again, you could extend the 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 conclusion:

 Before. canWrite? true Set Writable False. canWrite? false Set Writable True. canWrite? false 
+1
source

The read-only method may be useful, although it cannot be changed or hacked. So, if you cannot create an inaccessible file, you can convert its contents to incomprehensible due to some types of encryption algorithms. For example, AES can be so useful

0
source

Source: https://habr.com/ru/post/1398756/


All Articles