'Java.io/Android File() instance vs actual file-system object, when is it created?
I'm having a bit of trouble clarifying the documentation around Java.io.File.
From Android docs:
Instances of this class may or may not denote an actual file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition
To my understanding the File object is much like a path/pointer to a location in the file-system structure.
The bit about partitions is confusing me so,
When I call:
File file = new File(getFilesDir(),"myFileName.txt")
Is a new block of hard disk space being allocated for a new file-system object called "myFileName.txt" or not? ie.
Memory:
Documents
hats.png
After calling constructor:
Memory: OR Memory Partition Land:
Documents Documents myFileName.txt
hats.png hats.png
myFileName.txt
If not how/when does this file-system object called "myFileName.txt" get physically created?
Solution 1:[1]
A Java File is only a reference to a file, whether it exists or not. The method you show generates a new File using the specified directory while including the proper path separators automatically.
You can check file.exists() to see if the file you're referencing actually exists on the system or not.
The File doesn't get created until you open it.
You can also use file.createNewFile(); which has the same effect as calling touch at the command line.
Solution 2:[2]
Whenever we create an object irrespective of classes, we always use an reference variable to refer the object that is the actual memory location inside heap.
Ex-Animal deer = new Animal();
Animal is class here and deer is reference variable of type Animal, and that deer is referring to animal class object. And definitely we need an reference variable if we want to manipulate our members(Member Variable And Member Functions).
But the statement below ->
File file = new File( getFilesDir() ,"myFileName.txt");
Here file refers to myFileName.txt file for sure , but there is no sure about whether this file exists physically or not?
Assumption-1 ->File doesn't exists in your directory(what getFilesDir() refers..) physically
if your program contains this statement only, this will never create the actual file,as we have different methods like createNewFile(),createTempFile(having some formal args) to create the actual file etc.
Assumption-2 ->File exists in your provided directory..
if you have created your file in first execution of your program, then in subsequent execution of your program, your reference variable file, will refer to an actual file.
Consider this program->
try
{
File myFile = new File("information.txt");
System.out.println("Does your File Exists.."+myFile.exists());
boolean returnValue = myFile.createNewFile();
System.out.println("Return Value Is "+returnValue);
}
catch(Exception ee )
{
ee.printStackTrace();
}
Execute the above code atleast 2 times,and note the difference..
And Remember when the file is physically created ,it will occupy space in memory..
For More Info Refer This api -> https://developer.android.com/reference/java/io/File
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mick |
| Solution 2 | procrastinator |
