简体   繁体   English

如何使用java以只读方式打开文件?

[英]How to open a file as read-only with java?

I have a openFile method that gets a File that's stored in the object and opens it with the default program for that file extension. 我有一个openFile方法,它获取存储在对象中的File,并使用该文件扩展名的默认程序打开它。

For example, if the file Book.xls is stored, openFile will launch excel in a windows desktop. 例如,如果存储了Book.xls文件, openFile将在Windows桌面上启动excel。

private void openFile() throws IOException {

        Desktop dt = Desktop.getDesktop();
        dt.open(shl.getCurrentTab().getFileDemo());
}

What I want to know is the following: how can I make the file that's stored read-only ? 我想知道的是: 如何将存储的文件设为只读 I don't want the original local file to be read-only as well, just when it's opened through through this method. 我不希望原始的本地文件也是只读的,就像通过这种方法打开它一样。

There is the method File#setWritable(boolean) you could use: 你可以使用File#setWritable(boolean)方法:

// get a file
File file = shl.getCurrentTab().getFileDemo();

// disallow write operations
file.setWritable(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM