首页 \ 问答 \ Java中的File.rename() - 它是一个原子操作吗?(File.rename() in Java - is it an atomic operation? [duplicate])

Java中的File.rename() - 它是一个原子操作吗?(File.rename() in Java - is it an atomic operation? [duplicate])

可能重复:
如何在Java中自动重命名文件,即使dest文件已经存在?

我正在处理一个可能在工作中被杀的过程。 我想重命名一个文件。 Java的重命名操作是原子操作吗?

我对Linux,HP-UX,Solaris和AIX感兴趣。


Possible Duplicate:
How to atomically rename a file in Java, even if the dest file already exists?

I'm working with a process the might be killed in middle of work. I want to rename a file. Is Java's rename operation an atomic operation?

I am interested in the case of Linux, HP-UX, Solaris, and AIX.


原文:https://stackoverflow.com/questions/6112802
更新时间:2024-01-21 16:01

最满意答案

您可以按如下方式转换数组:

var userMap = userdata1.reduce(function(rv, v) {
  rv[v.id] = v;
  return rv;
}, {});

这将为您提供一个将“id”值映射到原始对象的对象。 然后,您将访问如下值:

var someUser = userMap[ someUserId ];

此设置将比您的数组更有效,因为查找条目需要花费一定时间与“id”字符串本身的大小成比例(加上一点)。 在您的版本中,您必须搜索(平均)每个查找的一半列表。 对于一小组记录,差异将是不重要的,但如果你有数百或数千个记录,差异将是巨大的。

.reduce()函数在旧版浏览器中不可用, 但MDN文档站点上提供了一个填充补丁:

// copied from MDN
if ('function' !== typeof Array.prototype.reduce) {
  Array.prototype.reduce = function(callback, opt_initialValue){
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      // At the moment all modern browsers, that support strict mode, have
      // native implementation of Array.prototype.reduce. For instance, IE8
      // does not support strict mode, so this check is actually useless.
      throw new TypeError(
          'Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var index, value,
        length = this.length >>> 0,
        isValueSet = false;
    if (1 < arguments.length) {
      value = opt_initialValue;
      isValueSet = true;
    }
    for (index = 0; length > index; ++index) {
      if (this.hasOwnProperty(index)) {
        if (isValueSet) {
          value = callback(value, this[index], index, this);
        }
        else {
          value = this[index];
          isValueSet = true;
        }
      }
    }
    if (!isValueSet) {
      throw new TypeError('Reduce of empty array with no initial value');
    }
    return value;
  };
}

You can transform your array as follows:

var userMap = userdata1.reduce(function(rv, v) {
  rv[v.id] = v;
  return rv;
}, {});

That will give you an object that maps the "id" values onto the original object. You would then access the values like this:

var someUser = userMap[ someUserId ];

This set up will be much more efficient than your array, because finding an entry takes an amount of time proportional to the size of the "id" strings themselves (plus a little). In your version, you have to search through (on average) half the list for each lookup. For a small set of records, the difference would be unimportant, but if you've got hundreds or thousands of them the difference will be huge.

The .reduce() function is not available in older browsers, but there's a fill-in patch available on the MDN documentation site:

// copied from MDN
if ('function' !== typeof Array.prototype.reduce) {
  Array.prototype.reduce = function(callback, opt_initialValue){
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      // At the moment all modern browsers, that support strict mode, have
      // native implementation of Array.prototype.reduce. For instance, IE8
      // does not support strict mode, so this check is actually useless.
      throw new TypeError(
          'Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var index, value,
        length = this.length >>> 0,
        isValueSet = false;
    if (1 < arguments.length) {
      value = opt_initialValue;
      isValueSet = true;
    }
    for (index = 0; length > index; ++index) {
      if (this.hasOwnProperty(index)) {
        if (isValueSet) {
          value = callback(value, this[index], index, this);
        }
        else {
          value = this[index];
          isValueSet = true;
        }
      }
    }
    if (!isValueSet) {
      throw new TypeError('Reduce of empty array with no initial value');
    }
    return value;
  };
}

相关问答

更多
  • 好的,我明白了。 我将数据放在EBS卷上并将其附加到实例上。 由于iPython允许您通过使用“!”将命令直接发送到操作系统,因此可以在此处指定的实例上安装卷: http : //docs.aws.amazon.com/AWSEC2/latest/ UserGuide / ebs-using-volumes.html 。 之前我还尝试过启用x-forwarding并在实例上安装浏览器并在该浏览器中运行ipython notebook。 然而,这被证明是非常缓慢的,尽管这可能只是因为我使用的是一个微实例,我已 ...
  • typeid的返回类型是std::type_info对象,它没有定义构造函数。 当make_pair从传入的参数中推导出其输出对的模板参数时,它会推导出std::pair 。 然后,由于上述原因,它无法创建所需的对。 您的另一行使用显式模板参数创建对: std::pair 。 这一次,你正在创建一个std::type_index对象,它有一个构造函数,它接受一个std::type_info - 正是你给它的东西。 所 ...
  • std::set存储并根据整个值搜索值。 因此,当你pair(key, null)进行find ,并且set包含pair(key, somevalue) ,它将找不到它,因为它们不相同。 如果你只想按键搜索,你需要一个std::map 。 正如您所说,这不会对值进行任何搜索或排序,因此您只能拥有一个具有给定key条目。 如果您只想通过键和键,值对(在同一数据结构的生命周期中的不同点处进行不同搜索)进行搜索/排序,那么您将需要更复杂的排列。 set s的map可以做你想要的: std::map
  • 请参阅Dropbox API : 401 Bad或过期的令牌。 如果用户或Dropbox撤消或过期访问令牌,则会发生这种情况。 要修复,您应该重新验证用户身份。 甚至更好的Dropbox最佳实践 : 如果被撤销访问,您的应用应采取预防措施。 用户可以禁用访问令牌(来自帐户页面),Dropbox管理员在滥用情况下撤消访问令牌,或者只是随着时间的推移而过期。 不幸的是,我无法告诉你令牌到期的时间跨度,但你应该定义准备请求新的访问令牌。 See Dropbox API: 401 Bad or expired to ...
  • 不,您无法使用AWS管理控制台更改实例的密钥对。 更改密钥的唯一方法是在这里解释: 更改ec2实例的密钥对 简而言之,SSH密钥是在安装期间由AWS在EC2实例中创建的文件。 一旦文件存在,AWS就不会触及它。 只有你能和它一起工作。 No, you can't change the key pair for an instance using AWS management console. The only way to change the key is explained here: Change k ...
  • 放松。 这不是非常困难,也不需要终止实例。 在更改密钥对之前,始终保持多个会话处于打开状态,这样,如果您犯了错误,可以使用其他ssh会话来恢复访问权限。 在此示例中,我假设用户为ubuntu ,但适用于任何用户。 如果出现问题,请带上机器的AMI 生成新的密钥对。 将私钥保存在安全的地方。 让公钥成为key.pub 编辑/home/user/ubuntu/.ssh/authorized_keys文件,并用key.pub的内容替换文件内容 使用新私钥尝试使用ssh进入计算机 如果您可以使用新密钥ssh ,请重 ...
  • 您正在考虑为此目的实现自定义的Map / Hash / Dictionary类。 基本上: class BookmarkObj { /* similar to steven's */ } class BookmarkStore { Dictionary byId; Dictionary byStartDate; Dictionary byEndDate; /* Borin ...
  • 首先,42个子表单的加载速度非常快,事实上,我已经做了这么多年,42个子表单的加载时间实际上是瞬时的。 因此,这表明读者可以忽略这里的一些评论,这些评论表明,与具有NEAR直接能力的Windows高性能桌面应用程序相比,基于脚本或基于文本的解释系统(如HTML)在某种类型的浏览器呈现系统中会更快地运行。直接写入视频图形卡。 请记住,如果您具有Windows桌面应用程序可以直接写入视频卡的简单和基本知识,那么很少有人会尝试比较并建议HTML中的渲染系统有任何真正希望在速度方面进行比较,如果我们要比较这里有两种 ...
  • 您可以按如下方式转换数组: var userMap = userdata1.reduce(function(rv, v) { rv[v.id] = v; return rv; }, {}); 这将为您提供一个将“id”值映射到原始对象的对象。 然后,您将访问如下值: var someUser = userMap[ someUserId ]; 此设置将比您的数组更有效,因为查找条目需要花费一定时间与“id”字符串本身的大小成比例(加上一点)。 在您的版本中,您必须搜索(平均)每个查找的一半列表。 ...
  • 您可以组合一个过滤掉重复项的函数和一个迭代对的函数: 首先,我们要注意消除列表中的重复后续条目。 由于我们希望保留顺序,以及允许彼此不相邻的副本,我们不能使用简单的集合。 因此,如果我们有一个坐标列表,如[(0, 0), (4, 4), (4, 4), (1, 1), (0, 0)] ,正确的输出将是[(0, 0), (4, 4), (1, 1), (0, 0)] 。 完成此任务的简单功能是: def filter_duplicates(items): """A generator that ignor ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。