File(File, String): This method is available in the java.io.File class of Java.
Syntax:
java.io.File.File(File parent, String child)
This method takes two arguments. This method creates a new File instance from a parent abstract pathname and a child pathname string.
Note: If a parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string.
Otherwise the parent abstract pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file.
Parameters: Two parameters are required for this method.
parent: The parent abstract pathname.
child: The child pathname string.
Throws:
NullPointerException - If the child is null
Approach 1: When no exception
Java
import java.io.File;import java.net.URI;import java.net.URISyntaxException;public class FileParent {public static void main(String[] args)throws URISyntaxException {URI uri = new URI("file:/D/hello");File parent = new File(uri);String child = "java";File file = new File(parent, child);System.out.println(file);}}
Output:
\D\hello\java
Approach 2: NullPointerException
Java
import java.io.File;import java.net.URI;import java.net.URISyntaxException;public class FileParent {public static void main(String[] args)throws URISyntaxException {URI uri = new URI("file:/D/hello");File parent = new File(uri);String child = null;File file = new File(parent, child);System.out.println(file);}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.io.File.<init>(File.java:362)
No comments:
Post a Comment