Maslor
Maslor

Reputation: 1910

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.

For example, if the file Book.xls is stored, openFile will launch excel in a windows desktop.

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.

Upvotes: 4

Views: 8326

Answers (1)

Andrew
Andrew

Reputation: 49606

There is the method File#setWritable(boolean) you could use:

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

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

Upvotes: 4

Related Questions