ruby 1.9簡単なファイル操作

1248 ワード


#   
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
" f << "and seven
" f << "years ago
" }

公式サイトFile API紹介
http://www.ruby-doc.org/core-1.9.3/File.html