Found 34489 Articles for Programming

File Path with double slash in Java

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

1K+ Views

The double slash in the file path is required as to create the ’\’ character , another ‘\’ needs to be added to escape it. 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 file = new File("C:\jdk11.0.2\demo1.txt");       System.out.println(file);    } }The output of the above program is as follows −OutputC:\jdk11.0.2\demo1.txtNow let us understand the above program.The file path of the file is provided with double slash and then it is printed. A code snippet that demonstrates this is ... Read More

Specify a path for a file or a directory in Java

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

3K+ Views

The path for a file can be obtained using the method java.io.File.getPath(). This method returns the abstract pathname in the form of a pathname string and it requires no parameters.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 file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");       System.out.println("Path = " + file.getPath());    } }The output of the above program is as follows −OutputPath = C:/jdk11.0.2/demo1.javaNow let us understand the above program.The path name of the file ... Read More

Get Absolute path of a file in Java

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

2K+ Views

The method java.io.File.getAbsolutePath() is used to obtain the absolute path of a file in the form of a string. This method requires no parameters.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 file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");       System.out.println("The absolute path name is: " + file.getAbsolutePath());    } }The output of the above program is as follows −OutputThe absolute path name is:/C:/jdk11.0.2/demo1.javaNow let us understand the above program.The absolute pathname of the file is ... Read More

Get file extension name in Java

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

526 Views

The file extension is the suffix that is attached to the computer file and it denotes the format of the file. A program that demonstrates getting the file extension name is given as follows −Example Live Demoimport java.io.File; public class Demo {    private static String fileExtension(File file) {       String name = file.getName();       if(name.lastIndexOf(".") != -1 && name.lastIndexOf(".") != 0)          return name.substring(name.lastIndexOf(".") + 1);       else          return "";    }    public static void main(String[] args) {       File file = new File("demo1.txt"); ... Read More

Change a file attribute to read only in Java

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

620 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

586 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 and download the “javatuples-1.2-dist.zip” as shown below −After downloading, unzip and ... Read More

Change a file attribute to writable in Java

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

436 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

469 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

181 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

75 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

Advertisements