美文网首页
习题 17 更多文件操作

习题 17 更多文件操作

作者: 南北东西总相随 | 来源:发表于2017-09-27 11:35 被阅读0次

习题 17 更多文件操作

# ex17.rb

from_file, to_file = ARGV

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

in_file = open(from_file)
indata = in_file.read

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

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

out_file = open(to_file, 'w')
out_file.write(indata)

puts "Alright, all done."

out_file.close
in_file.close

test.txt

This is a test file.

结果:

$ ruby ex17.rb test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 20 bytes long
Does the output file exist? false
Ready, hit RETURN to continue, CTRL-C to abort.

Alright, all done.

new_file.txt

This is a test file.

相关文章

网友评论

      本文标题:习题 17 更多文件操作

      本文链接:https://www.haomeiwen.com/subject/gltuextx.html