习题 17 更多文件操作

2017-09-27  本文已影响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.
上一篇 下一篇

猜你喜欢

热点阅读