一步一步學Ruby(二十一):檔案操作2

來源:互聯網
上載者:User

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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.