Java — Create Read only file or Mark a file as Read only | Code Factory

Java create Read only file

package com.example.java.programming.file;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;
/**
* @author code.factory
*
*/
public class CreateReadOnlyFileExample {
public static void main(String... args) {
try {
// File Path
Path filePath = Paths.get("test.txt");
// File permissions (Read only for USER, GROUP, and OTHER)
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("r--r--r--");
FileAttribute<Set<PosixFilePermission>> 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

package com.example.java.programming.file;import java.io.File;
import java.io.IOException;
/**
* @author code.factory
*
*/
public class CreateReadOnlyFileExample {
public static void main(String... args) {
try {
// File Path
File file = new File("test.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());
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Monix that is used for composing Asynchronous programs for Scala and Scala.js

Top 12 DevOps Tools for your DevOps Implementation Plan

IETF 108 Hackathon Goes Online!

Save time using Lombok + MapStruct

CS373 Spring 2021: Truman Byrd

Software engineering principles and best practices you should follow as a good developer

Software engineering principles

Super fast insert using Table Valued Parameter

Scala — Beau[T]y of Type classes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

MiniKube

Are Outsourced Java Developers Still in Demand — Let’s Check Out

Outsourced Java Developers Still in Demand

Features of Java 8 to 18

Root of Number (Java solution)