6

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 ?

Francis
  • 1,090
  • 7
  • 12
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

8 Answers8

7

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.

Francis
  • 1,090
  • 7
  • 12
4

I think the documentation is a little confusing: creating a new File object in Java does not mean creation of a new file in your file system. The File object is merely an abstract representation of file and directory pathname, it may or may not represent a real file on disk or on a network storage.

It is more or less equivalent to a String representing an address of something: when you write

String str = "1600 Pennsylvania Ave NW, Washington, DC 20500";

you create a string with an address of an existing building. There is no other connection between the string str that you created and The White House that happens to be located at that address.

The only difference between a File created with an existing path name and a file created with a non-existent path name is that the call of exists() on the former will return true, while the same call on the later would return false.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

A File is not directly linked to an actual file on the file system. If the file exists, it will point to that file. If the file doesn't exist, it will not create it. exist() will return false.

WilQu
  • 7,131
  • 6
  • 30
  • 38
0

The java.io.File class represents a path on some file system. It is not directly bound to a file. You are not opening a file when you create a File instance.

A File object is merely an object on the heap. Yes, it does have fields and methods that imply that this object represents a real file (or a directory: see the ambiguity?). You can create File objects for files/directories that do not exist: nothing will happen to the file system; the File instances will be created. After all, a File is just a descriptor.

Furthermore, you can create several File objects with different paths (esp. when one is absolute and others are relative from different parent paths), but they will all point to the same file/directory when they are actually evaluated (by opening a file with In/OutputStream, Reader/Writer; when checking with exists() or creating: createFile(), createDirectory().

Tadas S
  • 1,955
  • 19
  • 33
  • But I will later update the content of the file, I am wondering is the existing file will be replaced by a fresh file or not – Leem.fin Oct 31 '13 at 09:33
  • I guess these types of ambiguities is part of why Java 7 features the `Path` class. – Tadas S Oct 31 '13 at 09:34
0

This is a very confusingly named class.

A File object represents a file path, not an actual file. So when you create a File object you do not change anything on the filing system. Conceptually, it's no different to a String.

In java.nio, the class has been renamed to (the much more intuitive) Path.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

File f=new File("C://Existing_file")

above line indicates already existed file not the new one to be created file. File class instance always refers to IO operations and also it always refers to already consisted file

Raj
  • 600
  • 1
  • 5
  • 18
0

By creating new instance

File f= new File("ABC.txt");

This new object of file will point to a file named ABC.txt in your system, if present. If the ABC.txt file is not there, then the file object simply does not point to any file.

rekiem87
  • 1,565
  • 18
  • 33
Sam
  • 1
0

When a file is stored in a computer. The information related to the file is also stored( you can check it in the properties by right clicking on file). These are those information that is about the file.

So the File class object does nothing except represents the information about the file.

The File class object only provides you with information about the file and the same is stated in its definition.