有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAio/Android File()实例与实际文件系统对象,何时创建?

我在澄清有关Java的文档时遇到了一些困难。木卫一。文件

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

据我所知,文件对象非常类似于指向文件系统结构中某个位置的路径/指针

关于分区的部分让我很困惑

当我打电话时:

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”的文件系统对象是如何/何时物理创建的


共 (2) 个答案

  1. # 1 楼答案

    Java文件只是对文件的引用,不管它是否存在。显示的方法使用指定的目录生成新文件,同时自动包含正确的路径分隔符

    您可以检查file.exists()以查看您正在引用的文件是否确实存在于系统中

    只有打开该文件,才能创建该文件

    您还可以使用file.createNewFile();,其效果与在命令行中调用touch相同

  2. # 2 楼答案

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

    否。AFile将填充一个文件名称。没别的了。它不必是现有文件的名称

    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?

    不是

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

    执行以下操作之一时:

    • 调用File.createNewFile()(除非您喜欢长度为零的文件,否则大部分是不必要的)
    • FileOutputStreamFileWriter的构造函数中使用File,或在mode参数中使用RandomAccessFilew
    • 通过Path间接使用它来构造上面的一个或FileChannel