添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

We all occasionally run across a ‘situation’ on our computers that leaves us completely baffled, such as a file that has a size of zero, but how is that even possible? Today’s SuperUser Q&A post has the answers to a confused reader’s question.

有时我们所有人都在计算机上遇到一个“情况”,这使我们完全困惑,例如一个大小为零的文件,那怎么可能呢? 今天的“超级用户问答”帖子解答了一个困惑的读者的问题。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 ( The Question )

SuperUser reader Eugene S wants to know how a file’s size can be zero:

超级用户阅读器Eugene S想知道文件大小如何为零:

This is something that I ran into and could not think of a proper explanation. If I create an empty *.txt file on my computer and then look at its size, it shows a size of zero. How is that possible? I mean even if the file itself is empty, it must still have some size (even it if is just to store its own name). How can this be explained?

这是我遇到的,无法想到适当的解释。 如果我在计算机上创建一个空的 * .txt文件 ,然后查看其大小,则显示大小为零。 那怎么可能? 我的意思是,即使文件本身为空,它也必须具有一定的大小(即使只是为了存储自己的名称也是如此)。 如何解释呢?

How is it possible for a file to have a size of zero?

文件大小怎么可能为零?

答案 ( The Answer )

SuperUser contributors David Schwartz and Cort Ammon have the answer for us. First up, David Schwartz:

超级用户贡献者David Schwartz和Cort Ammon为我们提供了答案。 首先,David Schwartz:

It is possible because there really is no file. There is just a directory entry with a name and owner. The directory entry is logically distinct from the file. For example, the same file can have more than one name in more than one directory.

可能是因为实际上没有文件。 只有一个具有名称和所有者的目录条目。 目录条目在逻辑上与文件不同。 例如,同一文件在多个目录中可以有多个名称。

Unfortunately, the term file is not always used to mean precisely the same thing. But the file size logic comes from the model where a directory entry attaches a file to a directory, then the file names and related meta data are stored in the directory.

不幸的是,“文件”一词并不总是用来表示同一件事。 但是文件大小逻辑来自模型,其中目录条目将文件附加到目录,然后文件名和相关的元数据存储在目录中。

Followed by the answer from Cort Ammon:

随后是Cort Ammon的回答:

The semantic meaning of file size is different from the one you are using.

文件大小的语义含义与您使用的大小不同。

There are many file sizes which are meaningful. The most common one, and the one you are seeing here, is the number of bytes in the file. If the file is an empty text file, it may indeed contain zero bytes. This number is important to programmers because we often need to open a file, read all the data, and close it. We need to know how many bytes of data will be in the file so we can plan ahead.

有许多有意义的文件大小。 您在这里看到的最常见的是文件中的字节数。 如果该文件是一个空文本文件,则实际上可能包含零字节。 这个数字对程序员很重要,因为我们经常需要打开一个文件,读取所有数据,然后关闭它。 我们需要知道文件中将有多少字节数据,以便我们可以提前计划。

Another meaning arises from the way most file systems store data. Most file systems store data in blocks. For example, the file system may store data in 64 kB blocks, meaning it will never allocate anything which is not an even multiple of 64 kB. This sounds inefficient, but it can make bookkeeping quite a bit simpler, and often simpler means faster.

大多数文件系统存储数据的方式也产生了另一种含义。 大多数文件系统将数据存储在块中。 例如,文件系统可能以64 kB块存储数据,这意味着它将永远不会分配不是64 kB偶数倍的任何内容。 这听起来效率低下,但可以使簿记更加简单,并且通常更简单意味着更快。

A third meaning, which you are tugging at, would be the actual number of bits required on the hard-drive to describe the presence of a file. This includes information that is usually stored separately from the file. For instance, in Linux, the concept of the file name is stored in the inode for the directory containing the file. [ Based on input from other comments, this is (technically) stored in the directory’s data. When I wrote this, I was thinking of the small-directory case. Data smaller than 156 bytes can be stored directly in the inode. ] This is not a commonly used meaning because it is terribly hard to determine without knowing the tremendously deep inner workings of your file system (such as accounting for the space needed to store all the permissions on the file). However, if you have a 1,000,000 byte hard-drive and want to know how big of a file can fit on that hard-drive, this will be a very important meaning to you!

您要提到的第三个含义是硬盘驱动器上描述文件存在所需的实际位数。 这包括通常与文件分开存储的信息。 例如,在Linux中,文件名的概念存储在包含文件的目录的inode中。 [ 根据其他注释的输入,(技术上)将其存储在目录的数据中。 当我写这篇文章时,我想到的是小目录的情况。 小于156字节的数据可以直接存储在inode中。 ]这不是常用的含义,因为在不知道文件系统的内部深度非常深的情况下(例如,考虑存储文件上所有权限所需的空间),很难确定它。 但是,如果您有一个1,000,000字节的硬盘驱动器,并且想知道该硬盘驱动器上可以容纳多少文件,这对您来说将是非常重要的意义!

Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here .

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/229281/how-can-a-files-size-be-zero/

文件大小不为零,打开为空

做项目时遇到的错误:The supplied file was empty (zero bytes long),提供的文件 为空 。 项目里需要用多个方法对同一个流对象进行操作,当第一个方法进行操作时,一切正常。第二个方法再对这个流进行操作时,就无法从文件里读取信息了。 也没有什么很好的解决方法,只能曲线救国了。反正操作的是同一个文件,不如在每个方法内部都新建一个对应的流。解决问题。 如果大佬们有什么好的方法的话还请在评论区指出!! 创建或 打开 文件(也可用于 打开 管道,油槽,硬件设备等): HANDLE CreateFile( LPCTSTR lpFileName, // file name DWORD dwDesiredAccess, // access mode DWORD dwShareMode, ... 正常的windows、 linux 是可以的。但是有的平台限制就不可以。 因为有的文件是分块存储的。0字节,无法分块。 大部分的云存储都是分块存储,为了安全,一个文件往往不是存放在一个地方的。 还有IO。大文件会一直写,而分块之后就可以同时往多态服务器写。 这里有解释,这里记录一下实验。 一般情况下,HDD磁盘最小的管理单位是扇区(Sector,也叫Unit或者Block),扇区的大小是512Bytes,但 Linux 一次I/O是8个扇区即4096Bytes,因此 Linux (准确的说是ext2/ext3等文件系统)存取文件的最小单位即使4096Bytes(=8个Bloc... nutch无法下载中文文件的问题比如:http://www.example.com/中文.pdfwireshark抓包后发现是其无法正确encode中文。解决办法修改src/ java /org/apache/nutch/fetcher/Fetcher. java 加上编码功能附上Fetcher. java :/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreeme # 打开 一个workbook filename = r'H:\Timed_scheduling\data.xls' rb = xlrd.open_workbook(filename, encoding_override="utf-...