Ruby文件操作

来源:互联网 发布:淘宝网商城品牌男装 编辑:程序博客网 时间:2024/06/08 13:44

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
printwhile gets
 
pARGV.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
#文件是否存在
pFile::exists?("cnblogslink.txt" )# => true
 
#是否是文件
pFile.file?("cnblogslink.txt" )# => true
 
#是否是目录
pFile::directory?("c:/ruby" )# => true
pFile::directory?("cnblogslink.txt" )# => false
 
#文件权限
pFile.readable?("cnblogslink.txt" )# => true
pFile.writable?("cnblogslink.txt" )# => true
pFile.executable?("cnblogslink.txt" )# => false
 
#是否是零长度
pFile.zero?("cnblogslink.txt" )# => false
 
#文件大小 bytes
pFile.size?("cnblogslink.txt" )# => 74
pFile.size("cnblogslink.txt" )# => 74
 
#文件或文件夹
pFile::ftype("cnblogslink.txt" )# => "file"
 
#文件创建、修改、最后一次存取时间
pFile::ctime("cnblogslink.txt" )# => Sat Sep 19 08:05:07 +0800 2009
pFile::mtime("cnblogslink.txt" )# => Sat Sep 19 08:06:34 +0800 2009
pFile::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
>rubytest.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
原创粉丝点击