【问题标题】:Copy file (w/o using FileUtils)复制文件(不使用 FileUtils)
【发布时间】:2014-06-09 13:34:53
【问题描述】:

所以我认为 Marshal 是解决它的最佳方法。我加载文件并立即转储它。但我得到这个错误: “不兼容的封送文件格式(无法读取)格式版本需要 4.8;给定 91.112”

  def self.copy_file(src, dest)
    File.open(src) do |src_file|
      File.open(dest, 'w') do |dest_file|
        Marshal.dump(Marshal.load(src_file), dest_file)
      end
    end
  end

我无法使用 FileUtils,因为我使用了某个没有该库的“Ruby 变体”。而且我不知道有任何独立且可再分发的免费 FileUtils.dll。即使是这样,我仍然希望我的脚本没有任何额外的 .dll 文件。 并且没有 FileUtils File.copy() 似乎不存在。

【问题讨论】:

  • 你只是想复制一个文件吗?
  • 是的,只是一个简单的文件副本。
  • 您要复制文本文件和二进制文件吗?

标签: ruby file io copy marshalling


【解决方案1】:

怎么样:

def self.copy_file(src, dest)
  File.write(dest, File.read(src))
end

或者对于没有File.write的更古老的ruby版本:

def self.copy_file(src, dest)
  File.open(dest, 'w') { |f| f.write(File.read(src)) }
end

【讨论】:

  • 您使用的是哪个 ruby​​ 版本?
  • Ruby 1.9.2 的精简版本/构建称为 RGSS。但是您刚刚进行的编辑解决了它:)。谢谢
  • 没问题。红宝石总有办法;)
  • readwrite 在类 IOIO#readIO#write)和 File.ancestors.first(2) => [File, IO] 中定义。
猜你喜欢
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
相关资源
最近更新 更多