读书人

ruby 1.9 容易的文件操作

发布时间: 2012-10-13 11:38:17 作者: rapoo

ruby 1.9 简单的文件操作

#读文件f = File.open("myfile.txt", "r")f.each_line do|line|puts "I read this line: #{line}"end


File.foreach("myfile.txt") do|line|puts "I read this line: #{line}"end


f = File.open("myfile.txt", "r")line = f.getsputs "The line I read is: #{line}"


#写操作File.open('filename','w') do |f|  f.puts linesend


#得到当前目录所有文件名    files = Dir.glob('*.rd')


#删除特定目录所有文件名Dir.glob('*.rd').each{|f| File.delete f}


open('myfile.out', 'a') { |f|  f << "Four score\n"  f << "and seven\n"  f << "years ago\n"}



官网File API介绍
http://www.ruby-doc.org/core-1.9.3/File.html

读书人网 >Ruby Rails

热点推荐