Get the String representation of the current File object in Java


The string representation of the current file object can be obtained using the method java.io.File.toString(). This method returns the abstract path name in the string form.

A program that demonstrates this is given as follows −

Example

 Live Demo

import 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("File: " + file.toString());
   }
}

The output of the above program is as follows −

Output

File: C:/jdk11.0.2/demo1.java

Now let us understand the above program.

The abstract path name is printed in the string form using the method java.io.File.toString(). A code snippet that demonstrates this is given as follows −

File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");
System.out.println("File: " + file.toString());

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements