gpt4 book ai didi

java - System.currentTimeMillis 如何在短时间内返回不同长度的多头?

转载 作者:行者123 更新时间:2023-11-29 08:27:01 25 4
gpt4 key购买 nike

我正在创建临时目录

Files.createTempDirectory(String.format("project-it-screenshots-%d",
System.currentTimeMillis())).toFile();

我假设 System.currentTimeMillis() 返回的 long 都具有相同的长度,因为具有不同长度的两个返回值必须来自不同的世纪。每天多次调用该方法后,我得到了

project-it-screenshots-15343380422205011345585140911467
project-it-screenshots-15343381444485756307954062111474
project-it-screenshots-15343386111604255833362898885857
project-it-screenshots-15343388402055473274775132302475
project-it-screenshots-1534338974370463603854456850155
project-it-screenshots-15343390449093279576023746896269
project-it-screenshots-15343398036346426501075034038757
project-it-screenshots-15343399888498609875231360701639
project-it-screenshots-15343401211515574289310135012571
project-it-screenshots-15343405563586337945661543042110
project-it-screenshots-15343419027757787065954653813302
project-it-screenshots-15343425353682119990056938907868
project-it-screenshots-15343429571018010152043055630277
project-it-screenshots-15343431608662744493593200651167
project-it-screenshots-15343436122094379676346949668973
project-it-screenshots-15343442103124076542522856351947
project-it-screenshots-15343539846818150078805796257638
project-it-screenshots-15343555678106536264262565060437
project-it-screenshots-15343809068582300780873443756155
project-it-screenshots-15344378068483088975615680511653
project-it-screenshots-15344421866441533761733546877922

其中 1534338974370463603854456850155 比其他数字短一位。这似乎与去除前导 0 无关,因为没有和尾随 0,因为一些长度较大的数字有它们,而另一些则没有。

我正在寻找一个解释,而不是一个解决方法(我将使用一个可以排序的格式化日期或添加 0 填充到带有 String.format 的数字> 这只会稍微改变时间戳)。

我在 Ubuntu 18.04 上使用 OpenJDK 8。

最佳答案

方法System.currentTimeMillis() 返回一个long,它的长度最多只能是19 位。但是您的数字字符串长度为 31-32 个字符。

调用Files.createTempDirectory()正在向您的文件名添加其他字符。

Creates a new directory in the default temporary-file directory, using the given prefix to generate its name.

(大胆强调我的)

This method works in exactly the manner specified by createTempDirectory(Path,String,FileAttribute[]) method for the case that the dir parameter is the temporary-file directory.

该方法指出:

The details as to how the name of the directory is constructed is implementation dependent and therefore not specified.

如果您希望名称是可预测的,那么您不能在 Files 中使用任何“createTemp”方法。如果您同意后缀不受您的控制,那么您可以选择如何创建前缀。但是使用 System.currentTimeMillis() 并不是路径名比另一个短的原因。必须是实现,没有指定。

在 Windows 10 上,我得到了类似的结果。

C:\Users\rgettman\AppData\Local\Temp\project-it-screenshots-15344437949188484678689730417470

关于java - System.currentTimeMillis 如何在短时间内返回不同长度的多头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com