听老歌 发表于 2011-11-21 16:18

ruby 1.9 简单的文件操作

ruby 1.9 简单的文件操作







Ruby代码1.#读文件   
2.f = File.open("myfile.txt", "r")   
3.f.each_line do|line|   
4.puts "I read this line: #{line}"
5.end
#读文件
f = File.open("myfile.txt", "r")
f.each_line do|line|
puts "I read this line: #{line}"
endRuby代码1.File.foreach("myfile.txt") do|line|   
2.puts "I read this line: #{line}"
3.end
File.foreach("myfile.txt") do|line|
puts "I read this line: #{line}"
endRuby代码1.f = File.open("myfile.txt", "r")   
2.line = f.gets   
3.puts "The line I read is: #{line}"
f = File.open("myfile.txt", "r")
line = f.gets
puts "The line I read is: #{line}"Ruby代码1.#写操作   
2.File.open('filename','w') do |f|   
3.f.puts lines   
4.end
#写操作
File.open('filename','w') do |f|
f.puts lines
endRuby代码1.#得到当前目录所有文件名   
2.    files = Dir.glob('*.rd')
#得到当前目录所有文件名
    files = Dir.glob('*.rd')Ruby代码1.#删除特定目录所有文件名   
2.Dir.glob('*.rd').each{|f| File.delete f}
#删除特定目录所有文件名
Dir.glob('*.rd').each{|f| File.delete f}官网File API介绍
http://www.ruby-doc.org/core-1.9.3/File.html

2gua 发表于 2011-11-21 16:50

这个很好的啊。

gr33n 发表于 2011-11-21 17:03

很好啊。学习了。
页: [1]
查看完整版本: ruby 1.9 简单的文件操作