Java open file read only one

How to make a File Read Only in Java

Making a file read only is very easy in java. In this tutorial, we will learn following three things.
1) How to make a file read only
2) How to check whether the existing file is in read only mode or not
3) How to make a read only file writable in java.

1) Changing file attributes to read only

To make a file read only, we can use setReadOnly() method of File class. It returns a boolean value which we can further use to verify whether the operation got successful or not, same way as I did in the below program. As you can see that in the below program, I am changing the file attributes to read only of file “Myfile.txt” which is present in “C drive” of my computer.

import java.io.File; import java.io.IOException; public class ReadOnlyChangeExample < public static void main(String[] args) throws IOException < File myfile = new File("C://Myfile.txt"); //making the file read only boolean flag = myfile.setReadOnly(); if (flag==true) < System.out.println("File successfully converted to Read only mode!!"); >else < System.out.println("Unsuccessful Operation!!"); >> >
File successfully converted to Read only mode!!

2) Check whether the file is writable or read only

In order to check the file attributes, we can use canWrite() method of file class. This methods returns true if the file is writable else it returns false. As I am performing the operation on the file “Myfile.txt” which I already set to read only in the previous program, I am getting output as “File is read only”.

import java.io.File; import java.io.IOException; public class CheckAttributes < public static void main(String[] args) throws IOException < File myfile = new File("C://Myfile.txt"); if (myfile.canWrite()) < System.out.println("File is writable."); >else < System.out.println("File is read only."); >> >

3) How to make a read only file writable in java

To make a read only file to writable file, we can use setWritable() method. This method can also be used to make a file read only.
file.setWritable(true) : To make file writable.
file.setWritable(false) : To make file read only.

import java.io.File; import java.io.IOException; public class MakeWritable < public static void main(String[] args) throws IOException < File myfile = new File("C://Myfile.txt"); //changing the file mode to writable myfile.setWritable(true); if (myfile.canWrite()) < System.out.println("File is writable."); >else < System.out.println("File is read only."); >> >

Источник

Читайте также:  Java system memory check

Making a File Read-Only in Java

Learn to create a new file and make the file read-only in Java. A read-only file can be opened for reading, but we cannot modify or delete the file contents. A read-only file or directory can be deleted if the file system permits.

The setReadOnly() method marks the file or directory specified in the path so that only read operations are allowed.

The method returns true if and only if the operation succeeded; false otherwise

File file = new File("c:/temp/testReadOnly.txt"); // Mark it read only boolean success = file.setReadOnly();

2. Using File.setWritable(false)

The setWritable() is a convenient method to set the owner’s write permission for this abstract pathname.

It returns true if the operation succeeded. The operation will fail with SecurityException if the user does not have the required permissions.

File file = new File("c:/temp/testReadOnly.txt"); // Mark it read only boolean success = file.setWritable(false);

3. Check if the File is Read-Only or Writable

In order to check if the file is writable or read-only, we can use canWrite() method of File class. This method returns:

File file = new File("c:/temp/testReadOnly.txt"); System.out.println("File is writable : " + file.canWrite()); // true // Mark it read only boolean success = file.setWritable(false); System.out.println("File is writable : " + file.canWrite()); // false

Источник

Java Create Read only file or Mark a file as Read only

Java Create Read only file or Mark a file as Read only

In this quick and simple article, you’ll learn how to create a read only file or mark an existing file as read only.

Java create Read only file

The following example demonstrates how to create a read-only file using the createFile() method of java.nio.file.Files class.

import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Set; public class CreateReadOnlyFileExample  public static void main(String[] args)  try  // File Path Path filePath = Paths.get("foo.txt"); // File permissions (Read only for USER, GROUP, and OTHER) SetPosixFilePermission> permissions = PosixFilePermissions.fromString("r--r--r--"); FileAttributeSetPosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(permissions); // Create a file at the given file path with the given attributes Files.createFile(filePath, fileAttributes); System.out.println("Read only file created successfully"); > catch (FileAlreadyExistsException e)  System.out.println("File already exists"); > catch (IOException e)  System.out.println("An I/O error occurred: " + e.getMessage()); > catch (SecurityException e)  System.out.println("No permission to create file: " + e.getMessage()); > > >

Create a Read only file or Mark a file as Read only

There is another way to create a read only file or mark an existing file as read only using the java.io.File class’s setReadOnly() or setWritable() methods. The following example demonstrates the usage.

Note: you should try to use the previous method because that’s extensible and gives you more control.

import java.io.File; import java.io.IOException; public class CreateReadOnlyFileExample1  public static void main(String[] args)  try  // File Path File file = new File("bar.txt"); boolean isCreated = file.createNewFile(); if(isCreated)  System.out.println("File created successfully"); boolean success = file.setReadOnly(); // or file.setWritable(false); if(success)  System.out.println("File marked as read only"); > else  System.out.println("File could not be marked as read only"); > > else  System.out.println("File already exists"); > > catch (IOException e)  System.out.println("An I/O error occurred: " + e.getMessage()); > catch (SecurityException e)  System.out.println("No permission to create file: " + e.getMessage()); > > >

Источник

Java Create Read only file or Mark a file as Read only

Java Create Read only file or Mark a file as Read only

In this quick and simple article, you’ll learn how to create a read only file or mark an existing file as read only.

Java create Read only file

The following example demonstrates how to create a read-only file using the createFile() method of java.nio.file.Files class.

import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Set; public class CreateReadOnlyFileExample  public static void main(String[] args)  try  // File Path Path filePath = Paths.get("foo.txt"); // File permissions (Read only for USER, GROUP, and OTHER) SetPosixFilePermission> permissions = PosixFilePermissions.fromString("r--r--r--"); FileAttributeSetPosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(permissions); // Create a file at the given file path with the given attributes Files.createFile(filePath, fileAttributes); System.out.println("Read only file created successfully"); > catch (FileAlreadyExistsException e)  System.out.println("File already exists"); > catch (IOException e)  System.out.println("An I/O error occurred: " + e.getMessage()); > catch (SecurityException e)  System.out.println("No permission to create file: " + e.getMessage()); > > >

Create a Read only file or Mark a file as Read only

There is another way to create a read only file or mark an existing file as read only using the java.io.File class’s setReadOnly() or setWritable() methods. The following example demonstrates the usage.

Note: you should try to use the previous method because that’s extensible and gives you more control.

import java.io.File; import java.io.IOException; public class CreateReadOnlyFileExample1  public static void main(String[] args)  try  // File Path File file = new File("bar.txt"); boolean isCreated = file.createNewFile(); if(isCreated)  System.out.println("File created successfully"); boolean success = file.setReadOnly(); // or file.setWritable(false); if(success)  System.out.println("File marked as read only"); > else  System.out.println("File could not be marked as read only"); > > else  System.out.println("File already exists"); > > catch (IOException e)  System.out.println("An I/O error occurred: " + e.getMessage()); > catch (SecurityException e)  System.out.println("No permission to create file: " + e.getMessage()); > > >

Источник

Оцените статью