如何使 Java 中的文件夹不可读?

How to make a folder non-readable in Java?

我正在尝试创建一个文件夹结构 Test1/gotcha/Test3 我想让 Test1 不可靠。以下代码不起作用:

new File("D:\Test1\gotcha\Test3").mkdirs();
PrintWriter writer= new PrintWriter("D:\Test1\gotcha\Test3\testing.txt");
writer.write("Hello");
writer.close();

File f1= new File("D:\Test1");
f1.setReadable(false,false);
f1.setExecutable(false,false);

我仍然可以打开 Test1 文件夹。关于如何解决这个问题有什么建议吗?

无法在 Windows 中使文件夹不可读,这就是 setReadable() 在 windows 中不起作用的原因。

那是做不到的。

One way around is to change the file extension of files in that folder to a random name, so that even though the user tries to open it he cannot find an application to do it.

Even though he figures out that it can be opened with a text editor, We will encrypt it. So it becomes non-readable.

这只是一个 key-substitution 密码。您可以使用更复杂的算法,如 SHA 或 AES,使其无法破解。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Demo 
{
    public static void main( String[] args )throws IOException
    {   
        File file = new File("C://temp//Hello1.sddf");// a random extension.
        // creates the file
        file.createNewFile();
        // creates a FileWriter Object
        FileWriter writer = new FileWriter(file); 
        String data = "Hello world!";//hello world is the data.
        // Writes the content to the file
        writer.write(encrypt(data)); 
        writer.flush();
        writer.close();
    }

    private static String encrypt(String data) {// I used a simple cipher I advise to use any other encryption technique.
        // TODO Auto-generated method stub
        String op = "";
        for(int i = 0; i < data.length(); i++)
            op += (char)(data.charAt(i) - 5);
        return op;
    }
}