Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Constructors Explanation

Tags:

I was unable to understand the following file constructors.

public File(String parent, String child) and  public File(File parent, String child) 

What do the parameters parent and child mean for the file? When can I use these? I have done few programs related to file but I have never used these. I usually use

public File(String pathname) 

I have read Javadoc but I could not figure out when and how to use these constructors. Could someone please explain and give examples.

like image 618
Amarnath Avatar asked Aug 28 '12 18:08

Amarnath


People also ask

What is file constructor?

Constructor SummaryCreates a new File instance from a parent abstract pathname and a child pathname string. File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. File(String parent, String child)

What is file [] in Java?

In Java, a File is an abstract data type. A named location used to store related information is known as a File. There are several File Operations like creating a new File, getting information about File, writing into a File, reading from a File and deleting a File.

What does new file () do in Java?

File createNewFile() method returns true if new file is created and false if file already exists. This method also throws java.

What are file classes?

The File class contains several methods for working with the pathname, deleting and renaming files, creating new directories, listing the contents of a directory, and determining several common attributes of files and directories. It is an abstract representation of files and directory pathnames.


2 Answers

Explanation

The parent parameter is the parent directory of the child file name or relative file path.

Where parent is a File instance, it is a directory file. Where parent is a String, it's simply that directory in pathname terms.


Examples

Consider the following partial file system:

Documents     Homework     Classwork     Tests 

Rather than declaring each new file with "Documents\Subdir", you can declare the Documents directory as a file, and use it as the parent File of the other File instances, like so:

File documents = new File("Documents"); File tests = new File("Documents/Tests"); // new File(String);  File homework = new File(documents, "Homework"); // new File(File, String)  File classwork = new File("Documents", "Classwork"); // new File(String, String) 

Real-world application

In my experience, I've used applications that provide an API containing a method that returns the directory file in which third-party "plugins" are allowed to save/read files. Without the File(File, String) constructor, I would need to convert the directory file into an absolute path and append my target file to it.

In the following example, Environment.getProgramDirectory() returns the directory file in which permissions are granted.

File settingsFile = new File(Environment.getProgramDirectory(), "settings.txt"); 
like image 60
FThompson Avatar answered Nov 07 '22 01:11

FThompson


"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. " As specified on the API

like image 33
Sednus Avatar answered Nov 07 '22 01:11

Sednus