Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About File file = new File(path)

Tags:

java

java-io

The Java.iO.File document says the following words about its constructor which takes the pathname:

public File(String pathname)

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

But what if the pathname points to a file which is already existing?

File file = new File(PATH_TO_AN_EXISTING_FILE);

Does the above file instance represent a fresh new file (with the existing one be deleted?) Or does it represent the existing file ?

like image 655
Leem.fin Avatar asked Oct 31 '13 09:10

Leem.fin


People also ask

What is file file new file in Java?

The File. createNewFile() is a method of File class which belongs to a java.io package. It does not accept any argument. The method automatically creates a new, empty file.

What is a file path called?

Alternatively known as the pathname, the current path or path is the complete location or name of where a computer, file, device, or web page is located.

How do you create a file path?

If you're using Windows 10, hold down Shift on your keyboard and right-click on the file, folder, or library for which you want a link. If you're using Windows 11, simply right-click on it. Then, select “Copy as path” in the contextual menu.

What is an example of a file path?

For example, if the file path is D:sources , the current directory is C:\Documents\ , and the last current directory on drive D: was D:\sources\ , the result is D:\sources\sources . These "drive relative" paths are a common source of program and script logic errors.


1 Answers

What the documentation says is that it will create a new File instance. This mean it will create a new instance in memory of the File class.

This object will point to a file on you file system. However, if the file exists, it will not create a new file.

like image 62
Francis Avatar answered Oct 02 '22 13:10

Francis