在Java中设置可读和写不起作用

时间:2019-04-01 13:59:54

标签: java

我为Java应用程序制作了一个exe包装程序,该程序要求输入管理密码并使用setReadable(true)方法更改管理文件的权限。该方法返回true,即操作成功,但文件仍保留管理权限但没有任何反应

1 个答案:

答案 0 :(得分:0)

我正在回答我的问题,以便更好地查看代码。

import java.io.*;

public class Installer

//Runs with administrator Privillages

{

    public static void main(String[]args)
    {
        boolean b=new File("C:/Program Files/ ... ").setWritable(true,false);
        System.out.println("Successful in making writable : "b);
    }
}

输出:

Successful in making writable : true



class Installed
//Runs without Administrative privillages
{
    public static void main(String[]args)
    {
        boolean b=new File("C:/Program Files/Page Book/Data.txt").canWrite();
        System.out.println("I can write : "b);//the conflict
        try{
        PrintStream ps=new PrintStream(new FileOutputStream("C:/Program Files/Page Book/Data.txt"));
        ps.println("...");
        ps.close();
        }catch(Exception e){e.printStackTrace();}
    }
}

输出

I can write : true

java.io.FileNotFoundException: C:\Program Files\Page Book\Data.txt (Access is denied)

at java.io.FileOutputStream.open0(Native Method)

at java.io.FileOutputStream.open(FileOutputStream.java:270)

at java.io.FileOutputStream.<init>(FileOutputStream.java:213)

at java.io.FileOutputStream.<init>(FileOutputStream.java:101)

at Installed.main(Installed.java:23)`