1. Introduction
The toString() method returns the pathname string of the given File object. This method is equivalent to using Java IO getPath) method.
2. Method signature
public String toString()
Parameters:
- method does not take any parameter
Returns
- true - if file or directory exists in the filesystem
Throws
- SecurityException - when we do not have access to the file or directory
3. Examples
3.1. Example code that prints the results of toString
method on several File
objects
package com.frontbackend.java.io;
import java.io.File;
public class FrontBackend {
public static void main(String args[]) {
try {
File file = new File("frontbackend.txt");
File test = new File("../frontbackend/test");
File txt = new File("./frontbackend.txt");
File tmp = new File("/tmp");
File empty = new File("./../tmp/./frontbackend/..");
System.out.println(file.toString());
System.out.println(test.toString());
System.out.println(txt.toString());
System.out.println(tmp.toString());
System.out.println(empty.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output:
frontbackend.txt
../frontbackend/test
./frontbackend.txt
/tmp
./../tmp/./frontbackend/..
4. Conclusion
In this article, we presented toString
method used for printing the full path of the given File
object. This method is returns the same result as getPath()
method.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}