昊天SEO

练习17: 更多文件操作–笨方法学ruby在线

现在让我们再学习几种文件操作。我们将编写一个 Ruby 脚本,将一个文件中的内容拷贝到另一个文件中。这个脚本很短,不过它会让你对于文件操作有更多的了解。

from_file, to_file = ARGV
script = $0

puts "Copying from #{from_file} to #{to_file}"

# we could do these two on one line too, how?
input = File.open(from_file)
indata = input.read()

puts "The input file is #{indata.length} bytes long"

puts "Does the output file exist? #{File.exists? to_file}"
puts "Ready, hit RETURN to continue, CTRL-C to abort."
STDIN.gets

output = File.open(to_file, 'w')
output.write(indata)

puts "Alright, all done."

output.close()
input.close()

本文地址官网    新乡网站建设,seo

分享到:更多 ()
a