ITeye博客

ruby 1.9 简单的文件操作

夜鸣猪 · 2011-11-21 00:23 · Ruby 1.9 · 编程语言 · Markdown


#读文件
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.gets
puts "The line I read is: #{line}"

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


#得到当前目录所有文件名
    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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。