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

1、打开读取文件

1
2
3
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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里写如下代码:

1
2
3
while  line = ARGF .gets
  print line
end

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
c:\studyruby>test.rb cnblogslink.txt cnblogslink2.txt
社区
新闻
社区
新闻
招聘
博问
小组
闪存
网摘
. NET 频道
社区
新闻
社区
新闻
招聘
博问
小组
闪存
网摘
. NET 频道

3、文件信息查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#文件是否存在
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、查找文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 }

以上内容得到以下输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
>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
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/09/19/1569918.html如需转载请自行联系原作者

王德水
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值