天天看點

Ruby檔案操作

作者:ITEER

一、檔案操作

f=File.new("#{File.dirname(__FILE__)}/temp.m", "w+")
f.puts("I am lmy")           

1、檔案模式

"r" :隻讀。從檔案開頭開始(預設模式)。

"r+" :讀寫。從檔案的開頭開始。

"w" :隻寫。将現有檔案截斷為零長度,或建立用于寫入的新檔案。

"w+" :讀寫。将現有檔案截斷為零長度,或建立用于讀取和寫入的新檔案。

"a" :隻寫。如果檔案存在,則在檔案結尾處開始;否則,建立一個新檔案以供編寫。

"a+" :讀寫。如果檔案存在,則在檔案末尾開始;否則,建立用于讀取和寫入的新檔案。

"b" :(僅限DOS/Windows)二進制檔案模式。可能與上面列出的任何關鍵字母一起出現。

2、讀取檔案

file=File.open("#{File.dirname(__FILE__)}/temp.m","r")
file.each { |line| print "#{file.lineno}.", line }
file.close
           

上面讀取檔案,并一行行輸出來。

3、建立、删除、重命名檔案

File.new("#{File.dirname(__FILE__)}/tal.txt", "w" )
File.rename("#{File.dirname(__FILE__)}/tal.txt", "#{File.dirname(__FILE__)}/tal_test.txt")
File.delete("#{File.dirname(__FILE__)}/tal_test.txt")           

二、目錄操作

1、建立目錄

Dir.mkdir("#{File.dirname(__FILE__)}/testDir")           

2、删除目錄

Dir.rmdir("#{File.dirname(__FILE__)}/testDir")           

3、查詢目錄裡的檔案

p=Dir.entries("#{File.dirname(__FILE__)}/testDir")
puts(p)           

4、周遊目錄

Dir.entries("#{File.dirname(__FILE__)}/testDir").each {
          |e| puts e
    }           

三、ARGV and ARGF

1、ARGV

在運作ruby腳本的時候,所有的參數會以Array的形式儲存到ARGV中。

2、ARGF

ARGF則會根據ARGV中的值一個一個的處理,每處理一個就從ARGV中移除一個,直到處理完所有的值。

四、檔案資訊查詢

檔案是否存在

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 2019
    p=File::mtime( "cnblogslink.txt" ) # => Sat Sep 19 08:06:34 +0800 2019
    p=File::atime( "cnblogslink.txt" ) # => Sat Sep 19 08:05:07 +0800 2019           

五、查找檔案

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 "正規表達式查詢"
    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 }           

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

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.           

七、檔案比較 複制等

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

2人點贊

iOS開放相關技術