Robert Simoes Robert Simoes - 2 months ago 6
Android Question

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?

EJP EJP
Answer

To my understanding the File object is much like a path/pointer to a location in the file-system structure.

No. A File encapulates a file name. Nothing more. It doesn't have to be the name of an existing file.

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?

Not.

If not how/when does this file-system object called "myFileName.txt" get physically created?

When you do one of the following:

  • call File.createNewFile() (mostly unnecessary unless you like zero-length files)
  • use the File in the constructor of a FileOutputStream or FileWriter, or RandomAccessFile with w in the mode parameter.
  • use it indirectly via a Path to construct one of the above or a FileChannel.
Comments