Programming Articles - Page 2858 of 3366

Change a file attribute to read only in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

862 Views

A file attribute can be changed to read-only by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise. The method java.io.File.canWrite() is used to check whether the file can be written to in Java and if not, then the file is confirmed to be read-only.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       boolean flag;       try {          File file = new File("demo1.txt"); ... Read More

How to run JavaTuples program in Eclipse?

Amit Diwan
Updated on 22-Aug-2023 12:15:43

870 Views

Tuples in Java are an ordered collection of objects of different types. To run Tuple in Java, you need to upload an external jar file. Here, we will be using Eclipse IDE to create a new Java Project and upload the JavaTuples external jar file. The JavaTuples jar file is to be downloaded. Let us first download the jar file, create an Eclipse project and import the downloaded jar file. Here are the steps − Step 1 − Download JavaTuples Jar library and save it on your system. Open the GitHib link github.com/javatuples/javatuples/downloads and download the “javatuples-1.2-dist.zip” as shown below ... Read More

Change a file attribute to writable in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

593 Views

The file attribute can be changed to writable using the method java.io.File.setWritable(). This method has a single parameter i.e. a boolean value that if true allows the file to be writable and if false disallows the file to be writable. Also, this method returns true if the operation is successful and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          file.setReadOnly();   ... Read More

Set file attributes in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

694 Views

One of the file attribute that can be set is to make the file read-only. This can be done by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise.The java.io.File.canRead() and java.io.File.canWrite() methods are used to check whether the file can be read or written to respectively.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");       ... Read More

Get the total space of this partition in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

245 Views

The method java.io.File.getTotalSpace() is used to obtain the total space in the form of bytes for the partition specified by the required abstract path name. This method requires no parameters and it returns the bytes for the partition i.e. its total space.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);             System.out.println("Total space ... Read More

Get the usable space of this partition in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

134 Views

The method java.io.File.getUsableSpace() is used to obtain the usable space in the form of bytes for the virtual machine on the partition specified by the required abstract path name. This method requires no parameters and it returns the bytes for the virtual machine on the partition.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);         ... Read More

Get the free space of this partition in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

448 Views

The method java.io.File.getFreeSpace() is used to obtain the free space in the form of unallocated bytes for the partition specified by the required abstract path name. This method requires no parameters and it returns the unallocated bytes for the partition.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {          System.out.println(r);          System.out.println("Free space = " + r.getFreeSpace());     ... Read More

List the file system roots in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

382 Views

The method java.io.File.listRoots() is used to list the file system roots in Java. This method requires no parameters. It returns the available file system roots in the form of an array of file objects and if the file system roots cannot be determined, it returns null.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);         ... Read More

Create a file and change its attribute to read-only in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

The attribute of a file can be changed to read-only by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       boolean flag;       try {          File file = new File("demo1.txt");          file.createNewFile();          flag = file.setReadOnly();          System.out.println("File is read-only?: " + flag);       ... Read More

Java program to create a file and sets it to read-only

karthikeya Boyini
Updated on 15-Nov-2024 18:42:46

211 Views

In this article, we will learn to use Java to make a file read-only and check if it can still be written. A file can be set to read-only by using the method java.io.File.setReadOnly(). This method requires no parameters and returns true if the file is set to read-only and false otherwise. The method java.io.File.canWrite() is used to check whether the file can be written in Java and if not, then the file is confirmed to be read-only. Problem StatementThe task is to write a Java program that makes the file read-only and then checks whether the file can still ... Read More

Advertisements