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 ARGVprint while getsp ARGV.class=begin輸出: 社區 新聞 社區 新聞 招聘 博問 小組 快閃記憶體 網摘 .NET頻道 Array=end
ARGF
我們在test.rb裡寫如下代碼:
while line = ARGF.gets print lineend
在命令列裡執行得到如下結果
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" ) # => truep File::directory?( "cnblogslink.txt" ) # => false#檔案許可權p File.readable?( "cnblogslink.txt" ) # => truep File.writable?( "cnblogslink.txt" ) # => truep File.executable?( "cnblogslink.txt" ) # => false#是否是零長度p File.zero?( "cnblogslink.txt" ) # => false#檔案大小 bytesp File.size?( "cnblogslink.txt" ) # => 74p File.size( "cnblogslink.txt" ) # => 74#檔案或檔案夾p File::ftype( "cnblogslink.txt" ) # => "file"#檔案建立、修改、最後一次存取時間p File::ctime( "cnblogslink.txt" ) # => Sat Sep 19 08:05:07 +0800 2009p File::mtime( "cnblogslink.txt" ) # => Sat Sep 19 08:06:34 +0800 2009p 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 "Regex查詢"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/binc:/ruby/ChangeLog.txtc:/ruby/docc:/ruby/libc:/ruby/LICENSE.txtc:/ruby/manc:/ruby/MANIFESTc:/ruby/miscc:/ruby/README.1stc:/ruby/ReleaseNotes.txtc:/ruby/ruby.icoc:/ruby/rubyopt.delc:/ruby/rubyw.icoc:/ruby/samplesc:/ruby/scitec:/ruby/sharec:/ruby/srcc:/ruby/uninstall.exe條件查詢binChangeLog.txtdoclibLICENSE.txtmanMANIFESTmiscREADME.1stReleaseNotes.txtruby.icorubyopt.delrubyw.icosamplesscitesharesrcuninstall.exe尋找某一類型檔案test.rbtest2.rbOpen 查詢libReleaseNotes.txtrubyopt.delsamplesuninstall.exe---------------------------...binChangeLog.txtdoclibLICENSE.txtmanMANIFESTmiscREADME.1stReleaseNotes.txtruby.icorubyopt.delrubyw.icosamplesscitesharesrcuninstall.exeRegex查詢------------------------c:/ruby/binc:/ruby/ChangeLog.txtc:/ruby/docc:/ruby/libc:/ruby/LICENSE.txtc:/ruby/manc:/ruby/MANIFESTc:/ruby/miscc:/ruby/README.1stc:/ruby/ReleaseNotes.txtc:/ruby/ruby.icoc:/ruby/rubyopt.delc:/ruby/rubyw.icoc:/ruby/uninstall.exe------------------------c:/ruby/ruby.icoc:/ruby/rubyopt.delc:/ruby/rubyw.icoc:/ruby/libc:/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