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
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
- ruby文件操作
- ruby 文件读写操作
- RUBY操作EXCEL文件
- ruby 文件操作
- RUBY文件操作
- ruby文件操作大全
- Ruby文件操作
- Ruby文件操作
- ruby文件操作大全
- ruby 操作大文件
- Ruby 的文件操作
- ruby 文件操作
- ruby文件操作
- Ruby文件操作
- RUBY-文件、目录操作
- ruby操作excel文件
- ruby 文件操作
- Ruby操作Excel文件
- java编程提高性能
- Android camera preview and take picture with V4l2
- hbase权威指南阅读随手笔记二之过滤器
- ASP.NET 页面对象模型
- 打印显示手机剩余电量
- Ruby文件操作
- LCD 细节调整的一点体会
- HttpClientUtil方便测试开发post和get请求
- Tomcat常用的优化技巧
- 求字符串的所有组合
- JS 校验函数 第一篇
- 第一个驱动的小过程 [LCD]
- SpecDD系列:敏捷项目管理原则
- java读取Excel数据