一步一步学Ruby(二十一):文件操作2
2009-09-19 11:11 敏捷的水 阅读(2713) 评论(0) 编辑 收藏 举报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
版权:本文版权归作者所有,转载需经作者同意。
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)