Ruby熱門檔案操作方法_ruby專題

來源:互聯網
上載者:User

一、建立檔案

複製代碼 代碼如下:

    f=File.new(File.join("C:","Test.txt"), "w+")
    f.puts("I am Jack")
    f.puts("Hello World")

檔案模式
"r" :Read-only. Starts at beginning of file (default mode).
"r+" :Read-write. Starts at beginning of file.
"w" :Write-only. Truncates existing file to zero length or creates a new file for writing.
"w+" :Read-write. Truncates existing file to zero length or creates a new file for reading and writing.
"a" :Write-only. Starts at end of file if file exists; otherwise, creates a new file for writing.
"a+" :Read-write. Starts at end of file if file exists; otherwise, creates a new file for reading and writing.
"b" :(DOS/Windows only.) Binary file mode. May appear with any of the key letters listed above

二、讀取檔案

複製代碼 代碼如下:

    file=File.open(File.join("C:","Test.txt"),"r")
    file.each { |line| print "#{file.lineno}.", line }
    file.close

三、建立、刪除、重新命名檔案
複製代碼 代碼如下:

    File.new( "books.txt", "w" )
    File.rename( "books.txt", "chaps.txt" )
    File.delete( "chaps.txt" )

四、目錄操作
1     建立目錄
複製代碼 代碼如下:

    Dir.mkdir("c:/testdir")
     #刪除目錄
     Dir.rmdir("c:/testdir")
     #查詢目錄裡的檔案
     p Dir.entries(File.join("C:","Ruby")).join(' ')
     #遍曆目錄
     Dir.entries(File.join("C:","Ruby")).each {
          |e| puts e
    }

1、ARGV and ARGF
複製代碼 代碼如下:

ARGV
    ARGV << "cnblogslink.txt"
    #The gets method is a Kernel method that gets lines from ARGV
    print while gets
    p ARGV.class

ARGF
    while line = ARGF.gets
     print line
    end


2、檔案資訊查詢
複製代碼 代碼如下:

    #檔案是否存在
    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

3、尋找檔案
複製代碼 代碼如下:

    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 "---------------------------"     
    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 }

3、查詢目錄及子目錄檔案

複製代碼 代碼如下:

    require "find"
Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|
  Find.prune if f == "."
  puts f
end

原型:ref.find( [ aName ]* ) {| aFileName | block }
prune:Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find::find.

4、檔案比較 複製等

複製代碼 代碼如下:

    require 'ftools'
    File.copy 'testfile', 'testfile1'  » true
    File.compare 'testfile', 'testfile1'  » true

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.