JAVAio/Android File()实例与实际文件系统对象,何时创建?
我在澄清有关Java的文档时遇到了一些困难。木卫一。文件
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
据我所知,文件对象非常类似于指向文件系统结构中某个位置的路径/指针
关于分区的部分让我很困惑
当我打电话时:
File file = new File(getFilesDir(),"myFileName.txt")
是否为名为“myFileName.txt”的新文件系统对象分配了新的硬盘空间块?即
Memory:
Documents
hats.png
调用构造函数后:
Memory: OR Memory Partition Land:
Documents Documents myFileName.txt
hats.png hats.png
myFileName.txt
如果不是这个名为“myFileName.txt”的文件系统对象是如何/何时物理创建的
# 1 楼答案
Java文件只是对文件的引用,不管它是否存在。显示的方法使用指定的目录生成新文件,同时自动包含正确的路径分隔符
您可以检查
file.exists()
以查看您正在引用的文件是否确实存在于系统中只有打开该文件,才能创建该文件
您还可以使用
file.createNewFile();
,其效果与在命令行中调用touch相同# 2 楼答案
否。A
File
将填充一个文件名称。没别的了。它不必是现有文件的名称不是
执行以下操作之一时:
File.createNewFile()
(除非您喜欢长度为零的文件,否则大部分是不必要的)FileOutputStream
或FileWriter
的构造函数中使用File
,或在mode
参数中使用RandomAccessFile
和w
李>Path
间接使用它来构造上面的一个或FileChannel
李>