代码改变世界

一步一步学Ruby(二十一):文件操作2

2009-09-19 11:11  敏捷的水  阅读(2712)  评论(0编辑  收藏  举报

1、打开读取文件

file = File.open( "cnblogslink.txt" )
file.each { |line| print "#{file.lineno}. ", line }
file.close

输出:

1. 社区
2. 新闻
3. 社区
4. 新闻
5. 招聘
6. 博问
7. 小组 
8. 闪存 
9. 网摘 
10. .NET频道

file.lineno显示的是行号

2、ARGV and ARGF

ARGV

ARGV << "cnblogslink.txt"

#The gets method is a Kernel method that gets lines from ARGV
print while gets

p ARGV.class

=begin
输出:
  社区
  新闻 
  社区
  新闻 
  招聘 
  博问 
  小组  
  闪存  
  网摘  
  .NET频道
  Array
=end

ARGF

我们在test.rb里写如下代码:

while line = ARGF.gets
 print line
end

在命令行里执行得到如下结果

c:\studyruby>test.rb cnblogslink.txt cnblogslink2.txt
社区
新闻
社区
新闻
招聘
博问
小组
闪存
网摘
.NET频道
社区
新闻
社区
新闻
招聘
博问
小组
闪存
网摘
.NET频道

3、文件信息查询

#文件是否存在
p File::exists?( "cnblogslink.txt" ) # => true

#是否是文件
p File.file?( "cnblogslink.txt" ) # => true

#是否是目录
p File::directory?( "c:/ruby" ) # => true
p File::directory?( "cnblogslink.txt" ) # => false

#文件权限
p File.readable?( "cnblogslink.txt" ) # => true
p File.writable?( "cnblogslink.txt" ) # => true
p File.executable?( "cnblogslink.txt" ) # => false

#是否是零长度
p File.zero?( "cnblogslink.txt" ) # => false

#文件大小 bytes
p File.size?( "cnblogslink.txt" ) # => 74
p File.size( "cnblogslink.txt" ) # => 74

#文件或文件夹
p File::ftype( "cnblogslink.txt" ) # => "file"

#文件创建、修改、最后一次存取时间
p File::ctime( "cnblogslink.txt" ) # => Sat Sep 19 08:05:07 +0800 2009
p File::mtime( "cnblogslink.txt" ) # => Sat Sep 19 08:06:34 +0800 2009
p File::atime( "cnblogslink.txt" ) # => Sat Sep 19 08:05:07 +0800 2009

4、查找文件

puts "查找目录下所有文件及文件夹" 
Dir["c:/ruby/*"].each {|x| 
      puts x
} 
 
puts "条件查询" 
Dir.foreach('c:/ruby') { 
    |x| puts x if x != "." && x != ".."
}
 
puts "查找某一类型文件"
Dir["*.rb"].each {|x| 
  puts x
 }
 
puts "Open 查询"
Dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}

puts "---------------------------"      
Dir.open('c:/ruby') { |d| d.each { |x| puts x } }


puts "正则表达式查询"
Dir["c:/ruby/ruby/[rs]*"].each{|x| puts x} 

puts "------------------------"
Dir["c:/ruby/[^s]*"].each{|x| puts x}

puts "------------------------"    
Dir["c:/ruby/{ruby,li}*"].each{|x| puts x} 

puts "------------------------"    
Dir["c:/ruby/?b*"].each{|x| puts x}        


puts "查找目录及子目录的文件"
require 'find'     
Find.find('./') { |path| puts path }

以上内容得到以下输出:

>ruby test.rb
查找目录下所有文件及文件夹
c:/ruby/bin
c:/ruby/ChangeLog.txt
c:/ruby/doc
c:/ruby/lib
c:/ruby/LICENSE.txt
c:/ruby/man
c:/ruby/MANIFEST
c:/ruby/misc
c:/ruby/README.1st
c:/ruby/ReleaseNotes.txt
c:/ruby/ruby.ico
c:/ruby/rubyopt.del
c:/ruby/rubyw.ico
c:/ruby/samples
c:/ruby/scite
c:/ruby/share
c:/ruby/src
c:/ruby/uninstall.exe
条件查询
bin
ChangeLog.txt
doc
lib
LICENSE.txt
man
MANIFEST
misc
README.1st
ReleaseNotes.txt
ruby.ico
rubyopt.del
rubyw.ico
samples
scite
share
src
uninstall.exe
查找某一类型文件
test.rb
test2.rb
Open 查询
lib
ReleaseNotes.txt
rubyopt.del
samples
uninstall.exe
---------------------------
.
..
bin
ChangeLog.txt
doc
lib
LICENSE.txt
man
MANIFEST
misc
README.1st
ReleaseNotes.txt
ruby.ico
rubyopt.del
rubyw.ico
samples
scite
share
src
uninstall.exe
正则表达式查询
------------------------
c:/ruby/bin
c:/ruby/ChangeLog.txt
c:/ruby/doc
c:/ruby/lib
c:/ruby/LICENSE.txt
c:/ruby/man
c:/ruby/MANIFEST
c:/ruby/misc
c:/ruby/README.1st
c:/ruby/ReleaseNotes.txt
c:/ruby/ruby.ico
c:/ruby/rubyopt.del
c:/ruby/rubyw.ico
c:/ruby/uninstall.exe
------------------------
c:/ruby/ruby.ico
c:/ruby/rubyopt.del
c:/ruby/rubyw.ico
c:/ruby/lib
c:/ruby/LICENSE.txt
------------------------
查找目录记子目录的文件
./
./test2.rb
./test2
./test2/test2.rb
./test2/test.rb
./test1
./test.rb
./output
./films.txt
./cnblogslink2.txt
./cnblogslink.txt
./beans.txt
>Exit code: 0