- 论坛徽章:
- 0
|
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}"
- end
复制代码 Ruby代码- 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}"
- end
复制代码 Ruby代码- 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
- end
复制代码 Ruby代码- 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 |
|